EF6: The argument 'nameOrConnectionString' cannot be null, empty or contain only white space

Viewed 657

I am getting frustrated about this, as I have tried out everything. I have a backend project which uses EF6 (6.4.4) to connect to the database. There I have a DbContext:

public partial class WorkobjectContext : DbContext
{
    public WorkobjectContext()
        : base(DatabaseManager.ConnectionString)
    {
        // Set the initializer to null to disable intializtion otherwise EF tries to create the table if it does not exist (CreateDatabaseIfNotExists)
        Database.SetInitializer<WorkobjectContext>(null);
    }
    public virtual DbSet<DomainLayer.WorkobjectView> WorkobjectViews { get; set; }
}

Where DatabaseManager.ConnectionString is defined as following:

public static String ConnectionString => System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;

Now I have a UI project which uses this backend and it works perfect. I can use the backend and load data with EF6 from my oracle database.
The problem happens when I create a user control and reference WorkobjectContext:

var context = new WorkobjectContext();

Still here the UI-Application compiles successfully. But as I try to add this user control to somewhere else (another user control or a form) then the designer of Visual Studio 2015 does not work and shows the following error:

The argument 'nameOrConnectionString' cannot be null, empty or contain only white space. 

The callstack shows the following error message:

at System.Data.Entity.Utilities.Check.NotEmpty(String value, String parameterName)
at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at Workflow.Backend.InfrastructureLayer.Context.WorkobjectContext..ctor() in Workflow.Backend\InfrastructureLayer\Context\WorkobjectContext.cs:line 14
at Inbox.Modules.UcWorkobjectFilterPane..ctor() in Inbox\Modules\Workobjects\UcWorkobjectFilterPane.cs:line 26 

This is despite the fact that the project is compiled and run successfully.
I tried different things to overcome this problem:

  1. Assign the connectionstring directly to DatabaseManager.ConnectionString: public static String ConnectionString => "DATA SOURCE=XXX-XXX-XXX.XXX:3333/XXX.TSTFRA;PASSWORD=XXX;USER ID=XXX";
  2. Change the constructor of WorkobjectContext: public WorkobjectContext() : base(DatabaseManager.ConnectionString!= null ? DatabaseManager.ConnectionString: "DATA SOURCE=XXX-XXX-XXX.XXX:3333/XXX.TSTFRA;PASSWORD=XXX;USER ID=XXX")

None of them works and the designer fails and shows the mentioned error message.
What is the reason for this problem? How can I overcome that?

UPDATE:
I found out, if I do the following:

public WorkobjectContext() : base("DATA SOURCE=XXX-XXX-XXX.XXX:3333/XXX.TSTFRA;PASSWORD=XXX;USER ID=XXX")

and clean and rebuild the solution and restart visual studio (important to restart visual studio), then the error disappears. After restarting, I can undo the change:

public WorkobjectContext() : base(DatabaseManager.ConnectionString) 

and the error is not shown until I restart visual studio again. After restarting the visual studio with the undone changes, then the error reappears. What is the reason for this problem?

UPDATE:
My last achievement: If I change DatabaseManager.ConnectionString to the following, then the designer seems to work and does not show any error messages:

public static String ConnectionString => System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"] != null && !String.IsNullOrEmpty(System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString) ? System.Configuration.ConfigurationManager.ConnectionStrings["MainConnectionString"]?.ConnectionString :
    "DATA SOURCE=XXX-XXX-XXX.XXX:3333/XXX.TSTFRA;PASSWORD=XXX;USER ID=XXX";
0 Answers
Related