Entity Framework Core 2.0 添加迁移不生成任何东西

编程入门 行业动态 更新时间:2024-10-27 07:25:48
本文介绍了Entity Framework Core 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中安装了以下包:

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</GenerateRuntimeConf‌igurationFiles> </PropertyGroup>

她为此写了一篇博客:thedatafarm/data-access/the-secret-to-running-ef-core-2-0-migrations-from-a-net-core-or-网络标准类库/

She wrote a blog about it: thedatafarm/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 添加迁移不生成任何东西

本文发布于:2023-11-14 03:12:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:东西   Framework   Entity   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!