简体   繁体   中英

EF Core DbContext in .net standard 2.0 library



I'm working with my .Net Core 2.0 Application - I've managed to split it into several projects but...
I've got problem with: how to use DbContext from inside another assembly - .net standard 2.0 library.

Inside my ConfigureServices:

services.AddDbContextPool<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MsSql"), sqlOption => sqlOption.MigrationsAssembly("NetStandardAssemblyName")));


And I've got this error message:


I've found some solution here:
https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/
- but it will not work in my case. I do not want to copy configuration and connection strings from my .net core application.

I was trying to add multiple target frameworks to my project containing context like it was suggested in some other solutions on the internet, but it crashes my Visual Studio every time.

It turns out that you have to either implement IDesignTimeDbContextFactory or you have to update how you configure your application (use new convention for .net core 2.0), by moving most of your configuration from Startup class to Program class where you can build IWebHost , because:

.Net Core 2.0 while creating migrations actually start your application
- Then it searches for IDesignTimeDbContextFactory and if it could not be found
- It searches for IWebHost configuration
and then it uses one of those to do some ef core magic scaffolding and migrations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM