0

I am totally new to developing technology ASP.NET MVC + Entity Framework 4 5 (DataBase First). I'm in a problem that even after hours of research I have not found a solution.

On my application need that each client has their own database, which will be selected after login.

Create a mapping (edmx) for each base? I have to change the DbContext or connection string lasts execution?

I have no idea where to start. Can I create a mapping (edmx) for each database and change DbContext or connection string in runtime?

Thanks All.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • possible duplicate of [Pass connection string to code-first DbContext](http://stackoverflow.com/questions/4805094/pass-connection-string-to-code-first-dbcontext). Or http://stackoverflow.com/q/7599455/861716. Or http://stackoverflow.com/q/12284291/861716. – Gert Arnold May 17 '13 at 18:22

2 Answers2

0

When you're creating your DbContext you can just pass in the name of connectionstring in your web.config:

var db = new MyDbContext("NameOfConnectionStringInWebConfig");
Kenneth
  • 28,294
  • 6
  • 61
  • 84
0

You can pass a connection string Name, or Db Connection details (SqlConnection) If you have a multi DB concept where the the DBs are added at runtime, the DB Connection approach is useful since the App.config doesnt have the connection Name.

You can of course add the connection names to app.config, or try the DB connection approach. it gets fun to get that working when you get to the migration topic.

public class MyDbContext : DbContext  
//ctor
    public MyDbContext(string connectionName) : base(connectionName){  }

    public MyDbContext(DbConnection dbConnection, bool contextOwnsConnection)
        : base(dbConnection, contextOwnsConnection) { }
phil soady
  • 11,043
  • 5
  • 50
  • 95