ASP.NET vNext和EF7中的多个dbContexts
我正在努力与使用MVC 6和EF7的ASP.NET vNext构建Web系统相处。 我正在看这个教程: http : //stephenwalther.com/archive/2015/01/17/asp-net-5-and-angularjs-part-4-using-entity-framework-7
在页面上,您将看到如何将dbContext添加到项目中,并在启动文件中注册,如下所示:
// Register Entity Framework services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext();
上下文类看起来像这样:
public class MoviesAppContext:DbContext { public DbSet Movies { get; set; } }
这一切都很好,但现在我需要添加额外的DbContext。 虽然我不知道如何注册这个额外的上下文,以便它将被EF使用并可能在我的项目中使用。
假设我已经创建了一个像这样的新上下文:
public class MyNewSuper:DbContext { public DbSet Model1 { get; set; } public DbSet Model2 { get; set; } }
我如何继续注册它以便在我的项目中使用?
重要说明:自本文发布以来,配置Entity Framework 7服务的语法已经发生了变化,截至最近几轮测试结果都是准确的。 同样的想法仍应适用于新语法。
这是我一直在做的事情:
services.AddEntityFramework().AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString"))) .AddDbContext(options => options.UseSqlServer(Configuration.Get("StorageSettings:SQLConnectionString")));
其中StorageSettings:SQLConnectionString
是SQL Express数据库的连接字符串。 目前,我有DataContextA和DataContextB共享相同的数据库,但您可以将它们分开。 如果你想继续使用Configuration
方法(我不知道,非常酷!)你可以做这样的事情:
{ "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" } } }
同
services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext() .AddDbContext();
DataContextA
和DataContextB
都可以注入您的控制器:
public class MyController: Controller { public MyController(DataContextA dataA, DataContextB dataB) { // Do stuff } }
首先,在config.json之类的内容中,您可以添加yur连接字符串。 像下面这样的东西会起作用
"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;" } },
然后你有两个DBContexts。 假设:YourApp.AppDBContext和YourApp.AppIdentityDBContext
当然,您需要将这些包含在CS文件的顶部。
using YourApp.AppDBContext; using YourApp.AppIdentityDBContext;
例如,在startup.cs中,在启动方法中,配置构建器将如下所示:
var builder = new ConfigurationBuilder() .AddJsonFile("config.json") .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true); builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
在ConfigureServices方法中,您将添加DBContexts,如下所示:
services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration["Data:BlogData:ConnectionString"])) .AddDbContext(options => options.UseSqlServer(Configuration["Data:Identity:ConnectionString"]));
我希望这有帮助。 如果我可以进一步扩展,请随时给我一个喊叫。