Steven
Steven

Reputation: 18869

Can EntityFramework use a standard connection string of provider System.Data.SqlClient?

It seems that when you use Entity Framework, it automatically creates a connection string of provider System.Data.EntityClient.

So I end up with two connection strings connecting to the same database in my config:

<add connectionString="Server=S;Database=D;User ID=U;Password=P" name="DBConn" providerName="System.Data.SqlClient" />
<add connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=S;initial catalog=D;user id=U;password=P;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" name="MyEntities" />

Is there any way to consolidate these by having my entities connect to the first, standard connection string instead of the System.Data.EntityClient connection?

Upvotes: 2

Views: 157

Answers (1)

DVK
DVK

Reputation: 2792

You haven't said if you are using Code-First or Database-First, but you can pass a standard connection string into the DbContext class constructor.

public class MyDbContext : DbContext
{
    public MyDbContext() : base("My Connection String")
    {

I am not a fan of hard-coding connection strings, so I'll generally create a static class that has a static field that is initialized using whatever logic gets my connection string. In that case, your DbContext class would look something like:

public class MyDbContext : DbContext
{
    public MyDbContext() : base(SomeStaticClass.MyConnectionString)
    {

Upvotes: 2

Related Questions