2

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?

1 Answer 1

2

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)
    {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.