将Entity Framework Core迁移用于类库项目 [英] Using Entity Framework Core migrations for class library project

查看:70
本文介绍了将Entity Framework Core迁移用于类库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是已解决的问题,至少对于SQLite数据库。

This seem to be an issue that have been fixed already, at least for the SQLite databases.

我的解决方案包括3个项目:

My solution consists of 3 projects:


  1. WPF项目(默认启动项目)( .NET Framework 4.7 ),

  2. 核心项目,其中包含视图模型和非UI内容-类库项目( .NET Standard 2.0

  3. 关系项目保存了所有的Entity Framework数据层-我想将它们分开( .NET Standard 2.0

  1. WPF project (default startup project) (.NET Framework 4.7),
  2. "Core" project holding the view model and non-UI stuff - Class library project (.NET Standard 2.0)
  3. "Relational" project holding all of the Entity Framework data layer - I like to keep those separated (.NET Standard 2.0)

我已经在主WPF项目中安装了以下软件包:

I have installed the following packages into the main WPF project:

Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design

项目2和3在我的主要WPF项目中被引用。因此,基本上,对于EF来说,解析DbContext就足够了。

Projects 2 and 3 are referenced in my main WPF project. So basically, it should be enough for the EF to resolve the DbContextes.

但是,这不是-运行 Add-Migration 结果为:

However, it's not - as running Add-Migration on my WPF project results in:

PM> Add-Migration "Initial"
No DbContext was found in assembly 'TestWPFProject'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

默认情况下,切换到项目 3 软件包管理器控制台的结果为:

Switching to project 3 as default in the Package Manager Console results in:

PM> Add-Migration "Initial"
Unable to create an object of type 'ClientDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ClientDataStoreDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

如何在我的类库项目和WPF项目中正确使用EF Core迁移? / strong>

How can I properly use EF Core migrations with my class library project and WPF project?

推荐答案

我复制了您的解决方案并发现了...解决方案:)

I reproduced your solution and found... a solution :)


  1. 核心项目-称为 ClassLibrary1

  2. 关系项目-称为 EFClssLibrary

  3. WPF应用程序项目-名为 WpfApp1

  1. "Core" project - called ClassLibrary1
  2. "Relational" project - called EFClssLibrary
  3. WPF App project - called WpfApp1

名称 ClassLibrary1

类型 .NET Standard 2.0类库

依赖项:无。

在我的测试解决方案中,它仅包含一个类,即名为 Person 的模型。

In my test solution, it contains only one class, a model called Person.

Person.cs

namespace ClassLibrary1
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }
}






2。关系项目



名称 EFClassLibrary

类型 .NET Standard 2.0类库

依赖项

  • ClassLibrary1
  • Microsoft.EntityFrameworkCore (v2.1.1)
  • Microsoft.EntityFrameworkCore.SqlServer (v2.1.1)
  • Microsoft.EntityFrameworkCore.Tools (v2.1.1)

该项目在我的测试解决方案中包含只有一个类:数据库上下文。

This project, in my test solution, contains only one class: the database context.

ClientDbContext.cs

using ClassLibrary1;
using Microsoft.EntityFrameworkCore;

namespace EFClassLibrary
{
    public class ClientDbContext : DbContext
    {
        const string connectionString = "Server=(localdb)\\mssqllocaldb;Database=ClientDb;Trusted_Connection=True;";

        public ClientDbContext() : base() { }

        public ClientDbContext(DbContextOptions<ClientDbContext> options) : base(options) { }

        public DbSet<Person> People { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(connectionString);
        }
    }
}



连接字符串



在此类中,定义了一个用于连接数据库的连接字符串(假定它是LocalDb SQL Server)。如果要将连接字符串放在配置文件中,则可以在解决方案中添加共享的配置文件,然后在 App.config 文件中引用该文件(有关更多信息notifyartion看看此页面

Connection string

In this class a defined an used the connection string to connect to the database (assuming it's LocalDb SQL Server). If you want to put the connection string in a config file, you could add a shared config file in your solution and then reference that file in your App.config file (for more informartion take a look at this page)

为了能够在此项目上添加迁移而无需将其他项目设置为启动项目,则必须设置目标框架。右键单击该项目,然后单击 Edit EFClassLibrary.csproj 条目。在< TargetFramework> netstandard2.0< / TargetFramework> 行之下,您应该添加另一行,以指定要定位的框架。要定位.NET Framework 4.7,您应该添加

In order to be able to add migrations on this project without setting as startup project other projects, you must set the target framework. Right click on the project and click on the Edit EFClassLibrary.csproj entry. Below the <TargetFramework>netstandard2.0</TargetFramework> line, you should add another line which specify which framework you want to target. To target the .NET Framework 4.7 you should add

<TargetFramework>net47</TargetFramework>

可以找到所有允许值的列表此处

A list of all allowed values can be found here.

我的< em.EFClassLibrary.csproj 将.NET Framework 4.7添加为目标后,看起来像下面的代码。

My EFClassLibrary.csproj look like the code below after adding the .NET Framework 4.7 as target.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <TargetFramework>net47</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
  </ItemGroup>

</Project>



添加迁移



现在您已经准备好了添加您的第一个迁移。打开软件包管理器控制台,并将 EFClassLibrary 设置为默认项目。另外,将该项目设置为启动项目(右键单击该项目,然后单击设置为启动项目条目)。

Adding migrations

Now you are ready to add your first migration. Open the Package Manager Console and set as default project the EFClassLibrary. Also, set that project as startup project (right-click on the project and click on the Set as startup project entry).

Type

PM> Add-Migration Initial

然后

PM> Update-Database






3。 WPF应用程序项目



名称 WpfApp1

类型:使用 .NET Framework 4.7 WPF应用程序

依赖项

  • ClassLibrary1
  • EFClassLibrary
  • Microsoft.EntityFrameworkCore (v2.1.1)

在此项目中,我没有添加文件。刚刚编辑了 MainWindow.xaml.cs 文件以检查一切是否正常。

In this project I added no files. A just edited the MainWindow.xaml.cs file to check that everything works correctly.

MainWindow。 xaml.cs

using ClassLibrary1;
using EFClassLibrary;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var db = new ClientDbContext();

            db.People.Add(new Person()
            {
                Name = "Omar"
            });

            db.SaveChanges();
        }
    }
}

希望它会有所帮助:)

这篇关于将Entity Framework Core迁移用于类库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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