.net core 注册多个dbcontext注意事项

.net core多数据库连接与注册:

When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather than a non-generic DbContextOptions parameter.”

意思是在DbContext各自的构造函数中指名options的类型,如下两个dbcontext

在start文件中添加数据库连接

利用IOC实现多数据连接如下:

System.InvalidOperationException
HResult=0x80131509
Message=The DbContextOptions passed to the StudentDbContext constructor must be a DbContextOptions<StudentDbContext>. When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather than a non-generic DbContextOptions parameter.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at IocDemoWebApp.Models.StudentDbContext..ctor(DbContextOptions options, IConfiguration configuration) in D:\Source\Repos\IocDemoWebApp\IocDemoWebApp\Models\StudentDbContext.cs:line 14

startup.cs

 

以上说明是当注册多个dbcontext里,需要在dbcontext的构造函数里加上对应的DbContextOptions<TContext>

 

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!