Chris Dixon
Chris Dixon

Reputation: 9167

Entity Framework - using the same DbContext with different connection strings

I'm currently trying to use the same DbContext (I have two databases, of identical structure) in my application. I'm not quite sure what I'm doing wrong, but here's my current code - hopefully it should be pretty obvious what I'm trying to do. I'm using EF Database First (which the error at the bottom seems not to suggest).

My context factory code:

public class HOLContextFactory
    {
        public static HOLDbEntities Create()
        {
            return new HOLDbEntities(); // Works
        }

        public static HOLDbQuoteEntities CreateQuote()
        {
            return new HOLDbQuoteEntities(); // Gives error
        }
    }

public partial class HOLDbQuoteEntities : HOLDbEntities
    {
        public HOLDbQuoteEntities()
            : base("HOLDbQuoteEntities") // This should send "HOLDbQuoteEntities" as the base connection string?! 
// Also tried "name=HOLDbQuoteEntities"
            {
            }
        }

Web.config connection strings:

<add name="HOLDbEntities" connectionString="metadata=res://*/HOLDbContext.csdl|res://*/HOLDbContext.ssdl|res://*/HOLDbContext.msl;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" />

<add name="HOLDbQuoteEntities" connectionString="metadata=res://*/HOLDbContext.csdl|res://*/HOLDbContext.ssdl|res://*/HOLDbContext.msl;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" /> // using diff database - same structure

Error I'm getting when using "HOLDbQuoteEntities" :

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception**

Upvotes: 5

Views: 10099

Answers (2)

Christopher Stevenson
Christopher Stevenson

Reputation: 2861

Entity Framework needs to use the actual entities object:

public class HOLContextFactory
{
    public static HOLDbEntities Create()
    {
        // default connection string
        return new HOLDbEntities(); 
    }

    public static HOLDbEntities CreateQuote()
    {
        // specified connection string
        return new HOLDbEntities ("HOLDbQuoteEntities"); 
    }
}

public partial class HOLDbEntities
{
    public HOLDbEntities(string connectionString)
        : base(connectionString) 
        {
        }
    }
}

Upvotes: 4

Guish
Guish

Reputation: 5160

I've done the same thing in one of my project. I am creating my entity context using metadata=res://*/

Try this:

<add name="HOLDbEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" />

<add name="HOLDbQuoteEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=<connstringdetails>" providerName="System.Data.EntityClient" /> 

Upvotes: 1

Related Questions