1

I am starting an MVC web app, using the new Asp.Net Identity bits, and will be using Entity Framework.

Identity requires a "normal" connection string as follows:

<add name="DefaultConnection" connectionString="data source=.;initial catalog=MyDB;integrated security=True;" providerName="System.Data.SqlClient" />

and Entity Framework comes out of the box with a connection string like this:

<add name="MyDBEntities" connectionString="metadata=res://*/Data.MyApp.csdl|res://*/Data.MyApp.ssdl|res://*/Data.MyApp.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I don't want to maintain 2 connection strings in my Web.Config.

My EF DBContext generated class looks like this:

public partial class MyDBEntities : DbContext
{
    public MyDBEntities()
        : base("name=MyDBEntities")
    {
    }

What is the simplest way to instantiate MyDBEntities using the DefaultConnection connectionstring? I understand that EF needs some of that extra stuff of course, and I appreciate there are programmatic ways to build the connection.

Do I need to create a separate partial class with a constructor so that it won't get blown away when I regenerate from the DB?

thanks!

1 Answer 1

1

You can use the identity connectionstring and then use EntityConnectionStringbuilder (https://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder%28v=vs.110%29.aspx) to build the connectionstring for your entity connection. You need to create a partial class that allows you to instantiate the DbContext from that connectionstring instead the named one which will just look the corresponding connectionstring from the app.config or web.config. Or you could modify the template generating your entitycontext class.

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.