简体   繁体   中英

.NET Framework MVC project referencing a .NET Standard 2.0 project that generates a EF Core dbcontext. Why doesnt it work?

I have a projects done with a CORE RAZOR PAGES web projects and two .net standard 2.0 class libraries. One is the Infrastructure class than has my DbContext.

So I was told I need to use .NET Framework Classic for the web interface instead of core. Since .NET Framework 4.7.2 AND .NET core 2.2 is valid for .NET standard 2.0 I thought I could have a new Web project but keep the two class libraries.

So I reference the two class libraries and use the console to run 'update-database'. The starup project is the .NET Framework MVC project and the 'Default Project' is the .NET standard 2.0 class library. But I get the error

Your startup project 'Sr.RazorFrame' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

So it is saying my project needs a CORE Nuget package but I can't do that since it is .NET Framework... I am confused … So Is there a Microsoft.EntityFrameworkCore.Design for STANDARD ?

but I can't do that since it is .NET Framework.

Actually, you could use Microsoft.EntityFrameworkCore.Design within a .NET Framework project.

So Is there a Microsoft.EntityFrameworkCore.Design for STANDARD ?

If you look into the Microsoft.EntityFrameworkCore.Design package( v2.2.6 ), you'll find that there's a net461 version and a netstandard2.0 :

├───build
│   ├───net461
│   └───netcoreapp2.0
├───lib
│   ├───net461
│   └───netstandard2.0
├───package
│   └───services
│       └───metadata
│           └───core-properties
└───_rels

So it would be fine to use it within a classic ASP.NET MVC WebApp.

To run ef-core tools in your .NET Framework projects, make sure you've added the following packages:

  1. Microsoft.EntityFrameworkCore.Design packages
  2. Microsoft.EntityFrameworkCore.Tools packages.

If you're using SqlServer , feel free to install a Microsoft.EntityFrameworkCore.SqlServer package too.

Finally, you could run Add-Migration and Update-Database within your .NET Framework project:

PM> Add-Migration InitialCreate -Project Your.Standard.Lib.That.Contains.DbContext
To undo this action, use Remove-Migration.
PM> Update-Database -Project Your.Standard.Lib.That.Contains.DbContext
Applying migration '20190905015739_InitialCreate'.
Done.
PM>

Although this approach works, I doubt whether it deserves. Because you could even run ef-core migrations within a pure .NET Standard2.0 library:

  1. Add an additional target framework name eg netcoreapp2.1 . See Tseng's answer
  2. Don't forget to add a reference to Microsoft.EntityFrameworkCore.Design . If you haven't install the ef-core tools, also install the Microsoft.EntityFrameworkCore.Tools
  3. Add a DbContext that has a parameter-less constructor, or create a DesignTime Factory. For more details, see official docs

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