2

Using DbContext (string nameOrConnectionString)

 2 years ago
source link: https://www.codesd.com/item/using-dbcontext-string-nameorconnectionstring.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using DbContext (string nameOrConnectionString)

advertisements

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:

  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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK