1

I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx data source to project.

I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF.

But in Model1.Designer.cs, every time I get error like "Member with same signature already declared".

I'm unable to solve this problem.

1
  • 1
    Doesn't VS generate a context with a constructor that takes a string parameter? Commented Dec 2, 2014 at 12:40

2 Answers 2

2

Yes, it works! All you need to change is the connection string.

And I have just tested it in order to satisfy my own curiosity.

Here are the steps that I took:

  1. Take an existing database and create a model for it.

  2. Create a new empty database.

  3. In SQL Management Studio right click the first database -> Tasks -> Export Data. Export all it's data to the newly created database.

  4. Remove some records from the second database.

  5. Write this code:

    TMS_MiscEntities db = new TMS_MiscEntities();
    TMS_MiscEntities dbCopy = new TMS_MiscEntities();
    dbCopy.Database.Connection.ConnectionString = db.Database.Connection.ConnectionString.Replace("initial catalog=TMS_Misc", "initial catalog=TMS_Misc_new");
    
    Response.Write(string.Format("DB 1 records: {0}<br/>", db.ZipCodes.Count()));
    Response.Write(string.Format("DB 2 records: {0}<br/>", dbCopy.ZipCodes.Count()));
    
  6. Check results:

    DB 1 records: 869164
    DB 2 records: 868709
    
  7. Conclude that it works :)

This is how my connection string looks:

<add name="TMS_MiscEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ws2008;initial catalog=TMS_Misc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
Sign up to request clarification or add additional context in comments.

5 Comments

i will surely test it by tomorrow. if it doesnt work i ll let u know my scenario.... thanks for the reply.
Not working. I'm using EF6. dbCopy.Database.Connection.ConnectionString..... I'm unable to resolve .Database. it shows error in there.
What errors? Also are you sure the database schema is actually the same? Can you try exporting an existing database to a newly created one?
I see. That error doesn't seem related to using multiple dbs. It's just EF.
1

I'm using Entity Framework 6.1.3. I have added a constructor to my DbContext that takes a string parameter. This string can be the name of the connection stored in your App.config or a full connection string. Something like this:

public partial class MyDBContext : DbContext
{
    public MyDBContext(string connectionString)
        : base(connectionString)
    {
    }

   // DbSets, OnModelCreating, etc 
}

In my case, I manage a multi-tenant application and I use a ContextFactory to build the proper connection string and return my initialized context.

public class ContextFactory
{
    public MyDbContext GetContext()
    {
        string connectionString;
        // do some stuff here
        return new MyDbContext(connectionString);
    }
}

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.