Is EF Core Add Migration Support for the .NET Standard Library?

We tried to run EF Core Migration in the .Net Standard 1.6 class library, but it fails. But the same thing happens in the .Net Core 1.1 class library.

Is EF migration supported in .NET STANDARD?

+18
c # asp.net-core entity-framework-core .net-standard
source share
5 answers

The documentation covers this case of knowing the problem / limitation when a DbContext is placed inside the netstandardx.y class netstandardx.y .

Workaround 1 - use the application as a launch project

If you have an existing .NET Core application or .NET Framework application (including the underlying ASP.NET web application), you can use it as a startup project. If not, you can create a new one, just for use with the .NET Command Line Tools. Specify a startup project that is an "executable application." Example: Console

 dotnet ef migrations list --startup-project ../MyConsoleApp/ 

Workaround 2 - Cross-tuning frames with

Add an additional target structure to the class library project. This may be a version of the .NET Core App or the .NET Framework. To make a .NET Core App project, add "netcoreapp1.0" for the project, as in the example below: XML

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>netcoreapp1.0;netstandard1.4</TargetFrameworks> </PropertyGroup> </Project> 

When targeting the .NET Framework, make sure you are targeting projects of version 4.5.1 or later. XML

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net46;netstandard1.4</TargetFrameworks> </PropertyGroup> </Project> 
+25
source share

For EF Core Package Manager Console Tools users who see the following errors:

The launch project "MyNetStandardLibrary" is focused on the ".NETStandard" framework. There is no runtime associated with this platform, and projects aimed at it cannot be executed directly. To use the Entity Framework Core Package Manager console tools with this project, add an executable project that targets the .NET Framework or .NET Core that references this project and set it as the project to run; or upgrade this project to cross-target the .NET Framework or .NET Core.

OR

Your target project “MyNetCoreApp” does not match your migration assembly “MyNetStandardLibrary”. Modify the target project or change the migration assembly.


The documentation reveals the cause of these errors:

The target project is the place where any files are added (or in some cases deleted). By default, the default project selected in the package manager console is selected for the target project, but you can also specify it using the -Project parameter.

When you run the project code, the launch project starts. By default, one set is used as a launch project in Solution Explorer. It can also be specified using the -StartupProject parameter.

In a nutshell, you need to configure your StartUp project to a project that has a .NET runtime (in this case, .NET Core), and then make sure you set your .NET Standard project as the Package Manager Console> Default Project.

CLI solution example:

 Add-Migration MyMigration -Project MyNetStandardLibrary -StartupProject MyNetCoreApp 

Solution without CLI:

  1. Right-click the .NET Core application in your project
  2. Click "Install as a startup project"
  3. Open the package manager console
  4. Select your .NET Standard project from the Default Project drop-down list in the Package Manager console
  5. Run the CLI command (Add-Migration, add dotnet ef migrations, etc.)
+12
source share

I have not tried with .Net Standard 1.6, but it works for 2.0.

Microsoft.EntityFrameworkCore.Tools.DotNet needs to be added to each of your class libraries that have DbContext . Right-click the project and select Edit *.csproj . Then add the following:

  <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" /> </ItemGroup> 

Here you can see a more in-depth guide: EF 7 Migrations with Multiple DBContexts

+2
source share

@Tseng, thanks! Here are the explicit instructions.

Edit project file:

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup> </Project> 

Then add the factory design:

  public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory<ActivitiesDbContext> { public ActivitiesDbContext CreateDbContext(string[] args) { DbContextOptionsBuilder<ActivitiesDbContext> builder = new DbContextOptionsBuilder<ActivitiesDbContext>(); var context = new ActivitiesDbContext( builder .UseSqlServer("Data Source=(local);Initial Catalog=Activities;Integrated Security=False;User ID=user;Password=pw;Connect Timeout=30;Encrypt=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;") .Options); return context; } } 

Then build.

Then open the command line, go to the project folder and run:

 dotnet ef migrations add InitialCreate 

Now there should be an automatically generated migration folder. Love it!

+1
source share

shamefully for Microsoft.

I ported my dbcontext to a console application in my dotnet kernel solution.

In my decision 5 projects.

then i run dotnet ef migrations add CreateDatabase

I use against code and dotnet cli.

0
source share

All Articles