实体框架核心使用多个DbContexts
我有一个问题,当我试图访问我PartsDbContext现场我得到以下错误:实体框架核心使用多个DbContexts
System.Data.SqlClient.SqlException: 'Invalid object name 'fieldName''
看来,这是因为我想使我的PartsDbContext使用与我的ApplicationDbContext相同的数据库,它与Identity一起使用。我需要知道如何设置第二个dbcontext以与使用/创建不同数据库的EF内核一起工作。
我试图创建第二个连接字符串,但让我这个错误:
System.Data.SqlClient.SqlException: 'Cannot open database "PartsDb" requested by the login. The login failed. Login failed for user 'DESKTOP-4VPU567\higle'.'
这里是我的代码:
appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-PrecisionCustomPC-b14db89e-86ad-4855-a17f-ac64a04339aa;Trusted_Connection=True;MultipleActiveResultSets=true",
"PartsConnection": "Server=(localdb)\\mssqllocaldb;Database=PartsDb"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
PartsDbContext。 cs
public class PartsDbContext : DbContext
{
public DbSet<PartsViewModels.Tower> Towers { get; set; }
public DbSet<PartsViewModels.Motherboard> Motherboards { get; set; }
public PartsDbContext(DbContextOptions<PartsDbContext> options)
: base(options)
{
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddEntityFramework()
.AddDbContext<PartsDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("PartsConnection")));
services.AddMvc();
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin"));
});
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
AdminController.cs
[Authorize(Policy = "RequireAdminRole")]
public class AdminController : Controller
{
private readonly PartsDbContext _context;
public AdminController(PartsDbContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
public IActionResult Towers()
{
var model = _context.Towers.ToList();
return View(model);
}
}
线var model = _context.Towers.ToList();
是其中所述错误被显示出来。
再次。我想设置我的PartsDbContext以使用EF-Core将自动创建数据库的方式与Entity Framework Core一起工作。
我想通了。这主要是因为我意外删除了Identity使用的数据库,我需要弄清楚如何恢复。
显然我的连接字符串没有问题。我只需要进入包管理器,然后键入以下命令顺序:
Add-Migration init -Context PartsDbContext
Update-Database -Context PartsDbContext
我发现了这一点,因为这是我必须做的就是我的ApplicationDbContext再次工作,事实证明,当您在Visual Studio中使用个人用户身份验证创建新的MVC Core Web应用程序时,将为您完成此步骤。
所以基本上添加更多DbContexts的步骤是:
- 创建的DbContext类
- 在appsettings.json
- 的的DbContext添加到您的配置创建为一个的DbContext连接字符串服务Startup.cs
- 在将使用它的控制器中设置DbContext。
- 打开包管理器并运行上面的2行。 (如果“-Context”不行试试“--context”
- 运行程序,让EntityFrameworkCore把其余的工作
您是否使用多个数据库的相同数据库? ,如何分隔EFMigrationsHistory表中每个上下文的迁移? – dotnethaggis
我正在使用多个数据库 –
如果在启动时有一个环境条件确定哪个appsettings.json连接字符串用于给定上下文会怎么样?哪一个下午控制台将要使用? – DiscipleMichael
你能TY更换'“PartsConnection”:“服务器=(的LocalDB )\\ mssqllocaldb; Database = PartsDb“'with'”PartsConnection“:”Server =(localdb)\\ mssqllocaldb; Database = PartsDb; Trusted_Connection = True“'? –