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="data source=SERVER;
initialcatalog=DB;persist security info=True;
user id=XXX;password=YYY;MultipleActiveResultSets=True;
App=EntityFramework"" 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="
data source=SERVER;initial catalog=DB;
persist security info=True;
userid=XXX;password=YYY;MultipleActiveResultSets=True;
App=EntityFramework""){}
if I try so, I have the 'System.ArgumentException': Keyword not supported: 'connectionstring'.
It is possible? How to do what i want?
The problem seems to be twofold:
connectionString
in the connection stringThe correct code should then become:
public DBEntities()
: base("metadata=res://*/DB.csdl|res://*/DB.ssdl|res://*/DB.msl;provider=System.Data.SqlClient;provider connection string='data source=SERVER;initial catalog=DB;persist security info=True;userid=XXX;password=YYY;MultipleActiveResultSets=True;App=EntityFramework'")
{}
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With