简体   繁体   中英

.Net Standard Library DbContext For EF Core and EF 6

I have a web app that runs on net48 and a couple of API's that run on netcoreapp3.1. I'm currently targeting nestandard2.0, netcoreapp3.1 and net48 in my .Net Standard Library. All of the apps share the same database so I'm trying to target EF 6 with my .Net app and EF core with my .Net Core API's. I was able to use EF Core in my .Net app but with some limitations (mainly user management). Is it possible to target both System.Data.Entity.DbContext and Microsoft.EntityFrameworkCore.DbContext in my DbContext class? I've been trying things like:

#if NETCOREAPP3_1
    public class MyContext: Microsoft.EntityFrameworkCore.DbContext
#elif NET48
    public class MyContext: System.Data.Entity.DbContext
#endif

I haven't seem to find a good solution for this yet. I even tried putting the entire class inside the preprocessor directives but nada. It may be impossible, so I'm ok with targeting EF Core, but it will be much easier from an architectural perspective if I can still use EF6 in my .Net app.

EF Core 3.1 and EF 6.4 both work in netcoreapp3.1 and net48. But EF Core 3.1 is getting quite old, and no later version supports .NET Framework.

So if you want to use a single DbContext use EF Core 6.4. It's even older, but it's more complete and stable than EF Core 3.1. But using EF 6 with .NET Core is probably a pain.

I thought about that but I'm concerned with EF's caching.

That is not a real concern. Your net48 Web App and .NET Core APIs are seperate applciations and don't share any objects. Each would have its own DbContext in any case.

So you should use EF 6.4 or EF Core 3.1 in your net48 web app, and EF 5 in your .NET Core Web API. You can make the DbContexts work alike so consuming code only has to change the namespace, but there's no really good way to use a single DbContext.

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