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.
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:
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.
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
andcreate 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:
6. Check results:
7. Conclude that it works :)
This is how my connection string looks: