1

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 ?

punkouter
  • 5,170
  • 15
  • 71
  • 116
  • I don't think you can reference a core dll inside a .net framework project. You'll need two versions of that dll, one for core and one for .net framework. – user10728126 Sep 04 '19 at 20:30
  • so is there a .net 2.0 standard version ? Or I guess that doesn't make sense... Maybe the simple way is to create the schema and seed using the core project and then switching to the .NET Framework project to use that? – punkouter Sep 04 '19 at 20:59
  • Just because a package has Core somewhere in the name doesn't necessarily mean it targets .NET Core or even supports .NET Core. – mason Sep 05 '19 at 04:13
  • I don't think you can reference core assemblies within a .net framework app and vise versa. It may be possible but you'll have limitations. You'll probably see the yellow triangle next to the reference. Create a new console app and you'll see there are two options, Console App (.Net Core) and Console App (.Net Framework). – user10728126 Sep 05 '19 at 12:58

1 Answers1

2

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 e.g. 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
itminus
  • 23,772
  • 2
  • 53
  • 88