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.

1

2 Answers 2

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

Comments

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) { }

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.