我是 EF 新手,正在尝试使用 VS 2017、.NET Core 2.0 和 EF 2.0 创建一个简单的测试解决方案,但我无法 add-migration 来创建迁移文件夹以及初始迁移.
I'm new to EF and am trying to create a simple test solution using VS 2017, .NET Core 2.0 and EF 2.0 but I can't get add-migration to create the migrations folder and the initial migration.
我创建了一个名为 Driver 的解决方案,其中包含两个 .Net Core 类库项目:
I have created a solution called Driver with two .Net Core class library projects:
Driver.Data
Driver.Domain
Driver.Data中安装了以下包:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Driver.Data 有一个名为 Driver.Context 的 cs 文件:
Driver.Data has a single cs file called Driver.Context:
using Driver.Domain;
using Microsoft.EntityFrameworkCore;
namespace Driver.Data
{
public class DriverContext : DbContext
{
public DbSet<Company> Companies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
"Server=(localdb)\mssqllocaldb; Database=Driver; Trusted_Connection=True;"
);
}
}
}
Driver.Domain 有一个名为 Company.cs 的 cs 文件:
Driver.Domain has a single cs file called Company.cs:
namespace Driver.Domain
{
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
我已将启动项目设置为 Driver.Data,并在 PM 控制台中将默认项目设置为 Driver.Data
I've set the startup project to Driver.Data and in the PM Console I've set the Default Project to Driver.Data
这是add-migration init -verbose的输出:
Using project 'Driver.Data'.
Using startup project 'Driver.Data'.
Build started...
Build succeeded.
C:Program Filesdotnetdotnet.exe exec --depsfile "C:Usersalex.florinDocumentsVisual Studio 2017ProjectsDriver runkDriver.DatainDebug
etcoreapp2.0Driver.Data.deps.json" --additionalprobingpath C:Usersalex.florin.nugetpackages --additionalprobingpath "C:Program Files (x86)Microsoft SDKsNuGetPackagesFallback" --additionalprobingpath "C:Program FilesdotnetsdkNuGetFallbackFolder" --fx-version 2.0 "C:Program FilesdotnetsdkNuGetFallbackFoldermicrosoft.entityframeworkcore.tools2.0.1 ools
etcoreapp2.0ef.dll" migrations add init --json --verbose --no-color --prefix-output --assembly "C:Usersalex.florinDocumentsVisual Studio 2017ProjectsDriver runkDriver.DatainDebug
etcoreapp2.0Driver.Data.dll" --startup-assembly "C:Usersalex.florinDocumentsVisual Studio 2017ProjectsDriver runkDriver.DatainDebug
etcoreapp2.0Driver.Data.dll" --project-dir "C:Usersalex.florinDocumentsVisual Studio 2017ProjectsDriver runkDriver.Data\" --root-namespace Driver.Data
没有错误,但没有生成任何内容.
There are no errors but nothing is being generated.
@JulieLerman 在我的复数视觉课程讨论中提供了答案.尝试使用 .NET Core 类库显然存在问题.成功的解决方法是将以下内容放在 DBContext 项目的 csproj 文件中:
@JulieLerman provided the answer when I asked in her pluralsight course discussion. There is apparently an issue with attempting this with the .NET Core class libraries. The successful workaround is to put the following in the csproj file of the DBContext project:
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
她为此写了一篇博客:http://thedatafarm.com/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-网络标准类库/
She wrote a blog about it: http://thedatafarm.com/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-net-standard-class-library/
另外,一定要把里面有DBContext的项目设置为启动项目
Additionally, be sure to set the project with the DBContext in it as the startup project
这篇关于Entity Framework Core 2.0 添加迁移不生成任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!