Usage of DbContext(string nameOrConnectionString)

c# entity-framework sql-server

Question

I want to make DB connection using constructor

DbContext(string nameOrConnectionString)

but i dont want to do it using 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 line in code

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

instate i want something like this directly in 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 so, I have the 'System.ArgumentException': Keyword not supported: 'connectionstring'.

It is possible? How to do what i want?

1
3
5/19/2015 8:20:05 AM

Accepted Answer

The problem seems to be twofold:

  1. You have included the attribute name connectionString in the connection string
  2. The " needs to be replaced with '

The correct code should then become:

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 of the elements a connection string consists of, and here for more details on Entity Framework connection string.

Old anwers, for reference:

The problem seems to be that you are including everything the web.config uses to define a connection string. The connection string is itself only what is contained between the quotation marks after the attribute connectionString, 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 of the elements a connection string consists of.

8
5/19/2015 9:00:05 AM

Popular Answer

In your Model.Context.tt, add the following under the constructor;

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)#>
<#
}
}
#>
}

After rebuilding, you will now have a second constructor taking a string argument connStr;

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

You can now 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
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow