实体框架核心2.0添加迁移不会生成任何内容 [英] Entity Framework Core 2.0 add-migration not generating anything

查看:66
本文介绍了实体框架核心2.0添加迁移不会生成任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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 中安装了以下软件包:

The following packages are installed in 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 Files\dotnet\dotnet.exe exec --depsfile "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.deps.json" --additionalprobingpath C:\Users\alex.florin\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft SDKs\NuGetPackagesFallback" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --fx-version 2.0 "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.1\tools\netcoreapp2.0\ef.dll" migrations add init --json --verbose --no-color --prefix-output --assembly "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.dll" --startup-assembly "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.Data\bin\Debug\netcoreapp2.0\Driver.Data.dll" --project-dir "C:\Users\alex.florin\Documents\Visual Studio 2017\Projects\Driver\trunk\Driver.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</GenerateRuntimeConf‌igurationFiles> 
</PropertyGroup>

她为此写了一个博客:
http://thedatafarm.com/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-net-standard-class-library/

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

这篇关于实体框架核心2.0添加迁移不会生成任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆