Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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**

like image 375
Chris Dixon Avatar asked Feb 26 '13 14:02

Chris Dixon


2 Answers

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) 
        {
        }
    }
}
like image 130
Christopher Stevenson Avatar answered Sep 20 '22 12:09

Christopher Stevenson


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" /> 
like image 25
Guish Avatar answered Sep 22 '22 12:09

Guish