Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of DbContext(string nameOrConnectionString)

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?

like image 736
Michail Avatar asked Dec 24 '22 19:12

Michail


2 Answers

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.

like image 61
Njål Nordmark Avatar answered Dec 27 '22 08:12

Njål Nordmark


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
}
like image 44
Dion V. Avatar answered Dec 27 '22 07:12

Dion V.