Using DbContext (string nameOrConnectionString)

I want to make a DB connection using a constructor

DbContext (string nameOrConnectionString)

but I don't want to do it with an application config file. like this

line in config file

  <add name="DBEntities" connectionString="metadata=res://*/DB.csdl|
    res://*/DB.ssdl|res://*/DB.msl;provider=System.Data.SqlClient;
    provider connection string=&quot;data source=SERVER;
    initialcatalog=DB;persist security info=True;
    user id=XXX;password=YYY;MultipleActiveResultSets=True;
    App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

      

and the line in the code

public DBEntities() : base("name=DBEntities"){}

      

instate i want something like this right in the code

 public DBEntities() : base("connectionString=metadata=
 res://*/DB.csdl|res://*/DB.ssdl|res://*/DB.msl;
 provider=System.Data.SqlClient;provider connection string=&quot;
 data source=SERVER;initial catalog=DB;
persist security info=True;
userid=XXX;password=YYY;MultipleActiveResultSets=True;
App=EntityFramework&quot;"){}

      

If I try I have "System.ArgumentException": keyword not supported: "connectionstring".

Maybe? How do I do what I want?

+3


source to share


3 answers


The problem seems to be twofold:

  • You have included the attribute name connectionString

    in the connection string
  • "must be replaced with"

Then the correct code should look like this:

public DBEntities()
    : base("metadata=res://*/DB.csdl|res://*/DB.ssdl|res://*/DB.msl;provider=System.Data.Sq‌​‌​lClient;provider connection string='data source=SERVER;initial catalog=DB;persist security info=True;userid=XXX;password=YYY;MultipleActiveResultSets=True;App=EntityFramew‌​‌​ork'")
{}

      

You can look here for more details on the elements that make up a connection string and here for more details on Entity Framework connection strings.



Old profiles, for reference:

The problem is you include whatever the web.config uses to define the connection string. The connection string itself is just what is contained between the quotes after the connectionString attribute, in your case data source=SERVER;initial catalog=DB;persist security info=True;userid=XXX;password=YYY;MultipleActiveResultSets=True;App=EntityFramework

.

Try this instead:

public DBEntities() : base("data source=SERVER;initial catalog=DB;
  persist security info=True;
  userid=XXX;password=YYY;MultipleActiveResultSets=True;
  App=EntityFramework"){}

      

Also, you can look here for more details on the elements that make up the connection string.

+4


source


In Model.Context.tt

add to the constructor as follows:

public <#=code.Escape(container)#>(string connStr)
    : base(connStr)
{
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
    this.Configuration.LazyLoadingEnabled = false;
<#
}

foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
{
// Note: the DbSet members are defined below such that the getter and
// setter always have the same accessibility as the DbSet definition
if (Accessibility.ForReadOnlyProperty(entitySet) != "public")
{
#>
    <#=codeStringGenerator.DbSetInitializer(entitySet)#>
<#
}
}
#>
}

      

Once restored, you now have a second constructor with an argument string

connStr

;



public DBEntities() : base("name=DBEntities"){}
public DBEntities(string connStr) : base(connStr){}

      

Now you can create your own connection string in your code and pass it to the constructor. Example

public string connStr = "your connection string";

using (var db = new DBEntities(connStr))
{
    //do things with db
}

      

+2


source


The problem is that the connection string was passed to the constructor. try this instead:

private const string _connectionString = @"data source=SERVER;initial catalog=DB;persist security info=True;user id=XXX;password=YYY;MultipleActiveResultSets=True";
public DBEntities() : base(_connectionString)
{
}

      

0


source







All Articles