Using Entity FrameWork with multiple MS SQLSERVER Databases

c#, sql-server

Solution

EF needs 2 different pieces of information to work with data from a database:

1) The database schema: This is included as compiled code in your application and cannot normally be changed at runtime.

2) The connection string: This is provided at runtime, normally from a config file.

In your case, all the databases have the same schema, so you can just model one database and it will work for all the others.

The piece you want to change is the connection string. This tells EF how to find the database and can be provided at runtime.

There is an overload of the `DbContext` constructor which takes a connection string as a parameter: `MSDN: DbContext Constructor (String)`

And there are even classes in the framework that help create connection strings for you:

`MSDN: EntityConnectionStringBuilder Class`

`MSDN: Connection String Builders`

Problem

I have searched back and forth but seemingly could not get a hold of what I need. I am sorry if this has been answered of late. A redirection to the discussion will do me good. This is the scenario. I have been instructed to move from Microsoft Visual Foxpro (MS is withdrawing support come 2015) to .Net C# by my boss. For the sake of good foundation and adoption of best practices, I have decided to first learn, piece pertinent information together, then start coding. This is the second year. We are a bureau company that offer payroll processing outsource services to over 50 clients. Each client currently has their own database. The databases have tables with completely identical structures. I am a newbie. Totally new to .net world. I had started off with raw SQL using datatables, datareaders but in my research I got some discussions discouraging this. Many were of the view that Entity Framework should serve the purpose. But one is allowed to mix approaches especially when complex queries are involved. Can someone point me to some 'good read' where I can implement Entity Framework with over 50 indentical databases. Each database is totally independent and has nothing to dowith any other. When the user logs in, they select which client they need to process payroll for, then EF points to that database.

Original source