Multiple dbContexts in ASP.NET vNext and EF7

I am trying to get along with building web systems using ASP.NET vNext using MVC 6 and EF7. I look at this tutorial: http://stephenwalther.com/archive/2015/01/17/asp-net-5-and-angularjs-part-4-using-entity-framework-7

On the page you will see how to add dbContext to the project and is registered in the startup file as follows:

// Register Entity Framework services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext<MoviesAppContext>(); 

And the context class is as follows:

 public class MoviesAppContext:DbContext { public DbSet<Movie> Movies { get; set; } } 

Everything works fine, but now I need to add an additional DbContext. Although I do not know how to register this additional context so that it is used by EF and can be used in my project.

Say I created a new context:

 public class MyNewSuper:DbContext { public DbSet<Model1> Model1 { get; set; } public DbSet<Model2> Model2 { get; set; } } 

How do I go to register it for use in my project?

+5
source share
2 answers

Important Note: The syntax for configuring Entity Framework 7 services has changed since it was published, which has been accurate since the last few beta rounds. However, the same idea should apply to the new syntax.

Here is what I did:

 services.AddEntityFramework().AddSqlServer() .AddDbContext<DataContextA>(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString"))) .AddDbContext<DataContextB>(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString"))); 

where StorageSettings:SQLConnectionString is the connection string for the SQL Express database. I currently have both DataContextA and DataContextB that use the same database, but you can save them separately. If you want to use the Configuration method (which I did not know about, pretty cool!), You can do something like this:

 { "Data": { "DefaultConnectionA": { "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContextADatabase;Trusted_Connection=True;MultipleActiveResultSets=true", "DefaultConnectionB": { "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContextBDatabase;Trusted_Connection=True;MultipleActiveResultSets=true" } }, "EntityFramework": { "DataContextA": { "ConnectionStringKey": "Data:DefaultConnectionA:ConnectionString" } "DataContextB": { "ConnectionStringKey": "Data:DefaultConnectionB:ConnectionString" } } } 

from

 services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext<DataContextA>() .AddDbContext<DataContextB>(); 

Both DataContextA and DataContextB can be entered into your controller:

 public class MyController: Controller { public MyController(DataContextA dataA, DataContextB dataB) { // Do stuff } } 
+14
source

First of all, in something like config.json you can add jura connect strings. Something like the following will work

  "Data": { "BlogData": { "ConnectionString": "Server=tcp:YourHostname.net,1433;Database=YourDatabaseName;User ID=YourDBUser@YourDomain ;Password=YourPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" }, "Identity": { "ConnectionString": "Server=tcp:YourHostname.net,1433;Database=YourDatabaseName;User ID=YourDBUser@YourDomain ;Password=YourPassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" } }, 

You have two DBC contexts. Say: YourApp.AppDBContext and YourApp.AppIdentityDBContext

Of course, you need to include them at the beginning of your CS file.

 using YourApp.AppDBContext; using YourApp.AppIdentityDBContext; 

In startup.cs, for example, in the startup method, your configuration builder would look like this:

  var builder = new ConfigurationBuilder() .AddJsonFile("config.json") .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true); builder.AddEnvironmentVariables(); Configuration = builder.Build(); } 

In the ConfigureServices method, you add your DBC texts as follows:

  services.AddEntityFramework() .AddSqlServer() .AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration["Data:BlogData:ConnectionString"])) .AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(Configuration["Data:Identity:ConnectionString"])); 

Hope this helps. Feel free to scream if I can expand this further.

+1
source

All Articles