c# - Using multiple Databases with single DbContext and Entites and generating Conn String at runtime -


i developing mvc 5 application. using single database ef6 database first approach , using dbcontext instance access database have 102 tables. declaring instance as:

private myentities db = new myentities();

now want allow multiple companies use application , matter have create new database each new company. achieved declaring constructor dbcontext follows:

public partial class newentities : dbcontext {     public newentities(string name)         : base(name)     {     } } 

and declaring instance as:

public newentities de = new newentities((configurationmanager.connectionstrings["newentities123"]).tostring()); 

then called

db.database.create();

and new database created successfully. in scheme have declare new connection string in web.config file each time want add database.

is there method connection string generated automatically per company name in config file , passed constructor create new database name?

moreover want access controller methods same "db" instance , dbcontext every company, same code can used companies. how can access single dbcontext , instance multiple databases?

i have looked answer this saying there no method. how can application work multiple users?

when working database-first, having edmx file mappings, can create , use database way.

add overload context constructor (as did), example:

public tenantdbcontainer(string connectionstring)     : base(connectionstring) { } 

then when need create database if not exists, using yourcontextinstance.database.createifnotexists();

var connectionstringtemplate =     @"metadata=res://*/models.tenantdb.csdl|res://*/models.tenantdb.ssdl|res://*/models.tenantdb.msl;" +     @"provider=system.data.sqlclient;" +     @"provider connection string=""data source=(localdb)\v11.0;" +     @"initial catalog={0};"+     @"user id={1};password={2};" +     @"multipleactiveresultsets=true;app=entityframework"";";  var tenanddbname = "database name based on tenant"; var tenantusername = "username based on tenant"; var tenantpassword = "password based on tenant"; var connectionstring = string.format(connectionstringtemplate, tenanddbname, tenantusername, tenantpassword); var db = new tenantdbentities(connectionstring); db.database.createifnotexists(); 

note:

  • this way in multi-tenant application can create database per tenant , use it.
  • you don't need change webconfig add connection string there, can create connection string @ run-time.
  • now can design structure to context based on tenant detection strategy.

as example simplify case, suppose have static method somewhere returns instance of context you, example:

public class dbhelper {     public static tenantdbentities getdbcontext(string tenantname)     {         var connectionstringtemplate =             @"metadata=res://*/models.tenantdb.csdl|res://*/models.tenantdb.ssdl|res://*/models.tenantdb.msl;" +             @"provider=system.data.sqlclient;" +             @"provider connection string=""data source=(localdb)\v11.0;" +             @"initial catalog={0};"+             @"integrated security=true;" +             @"multipleactiveresultsets=true;app=entityframework"";";          var tenanddbname = "tenantdb_" + tenantname;         var connectionstring = string.format(connectionstringtemplate, tenanddbname);         var db = new tenantdbentities(connectionstring);         db.database.createifnotexists();          return db;     } } 

and suppose tenant dettection strategy based on request url or else.

now example in productcontroller in index action can use way;

public actionresult index() {     var tenantname = "get tenant based on strategy, example tenant1 or tenant2";     var db= dbhelper.getdbcontext(tenantname)     var model= db.products.tolist();     return view(model); } 

when each tenant working action, connects database suitable tenant, , if database not exists, first creates uses it.

this way each tenant see own products.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -