5

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

2 Answers 2

4

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

1 Comment

This is actually how I did it in the end.
1

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" /> 

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.