在单独的项目中使用EF核心设置多个项目 [英] Setting up multiple projects with EF core in separate project

查看:56
本文介绍了在单独的项目中使用EF核心设置多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何设置包含多个项目的解决方案,一个项目是ASP Web API(设置为启动项目),另一个项目是API.Data项目(仅是标准类库).

How should I setup a solutions with multiple projects, one project is ASP Web API (set as startup project) and the other API.Data project (just standard class library).

运行 Add-Migration 后出现以下错误:

启动项目"API.Data"以框架".NETStandard"为目标.有没有与此框架相关联的运行时,并且针对它的项目无法直接执行.使用实体框架核心软件包使用此项目的Manager Console工具,添加一个可执行项目定位到引用此项目的.NET Framework或.NET Core,并将其设置为启动项目;或者,将该项目更新为跨目标.NET Framework或.NET Core.

Startup project 'API.Data' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core.

我应该在具有所有程序集的API.Data项目或主启动项目中安装 Microsoft.EntityFrameworkCore 吗?

Should I install Microsoft.EntityFrameworkCore in the API.Data project or the main startup project with all the assemblies?

我为API.Data项目安装了 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Design ,将主API项目设置为启动项目.

I installed Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design for the API.Data project, the main API project is set as the startup project.

我收到此错误:

您的启动项目"API"未引用Microsoft.EntityFrameworkCore.Design.该软件包是实体框架核心工具可以正常工作.确保您的启动项目是正确,安装软件包,然后重试.

Your startup project 'API' 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.

如果将启动项目更改为API.Data项目,则会出现此错误:

If I change the startup project to the API.Data project I get this error:

启动项目"API.Data"以框架".NETStandard"为目标.没有与此框架和项目关联的运行时定位它不能直接执行.使用实体框架Core Package Manager控制台工具与此项目一起,添加一个面向.NET Framework或.NET Core的可执行项目引用此项目,并将其设置为启动项目;或更新该项目可以跨目标.NET Framework或.NET Core.

Startup project 'API.Data' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core.

构建解决方案时,我将为所有程序集构建警告:

When I build solution I these build warnings for all the assemblies:

 Consider app.config remapping of assembly ...

csproj文件:

https://pastebin.com/fG7w81a8

推荐答案

Entity Framework Core 2.2 +

在包含 DBContext 的库项目中(我们将其称为 MyProject.Data ),请确保已安装这些软件包(您的版本可能会有所不同,可能需要将 SqlServer 更改为您用于数据库的任何内容):

In your library project containing the DBContext (let's call it MyProject.Data), make sure you have these packages installed (your versions will probably be different, also you may need to change SqlServer to whatever you're using for your DB):

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />

在我的 Startup.cs 中,我还指定了 Migration Assembly ,但我认为这不是必需的:

In my Startup.cs I also specified the Migration Assembly but I do not think this is necessary:

        services.AddDbContext<MyDataContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                serverOptions => serverOptions.MigrationsAssembly(typeof(MyDataContext).Assembly.FullName));

然后在 Package Manager控制台中,将目录更改为Service/Startup项目,例如:

Then in the Package Manager Console, change the directory to your Service/Startup project, for example:

cd .\src\MyProject.Service

然后根据您要执行的操作,仍然在 Package Manager控制台中:

Then depending on what you want to do, still in the Package Manager Console:

添加迁移:

dotnet ef migrations add MyMigrationName --startup-project ./ --project ../MyProject.Data/

更新数据库:

dotnet ef database update --startup-project ./ --project ../MyProject.Data/

生成脚本(上一次迁移):

dotnet ef migrations script --startup-project ./ --project ../MyProject.Data/

这篇关于在单独的项目中使用EF核心设置多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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