对类库项目使用 Entity Framework Core 迁移

编程入门 行业动态 更新时间:2024-10-24 18:20:20
本文介绍了对类库项目使用 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:

  • WPF 项目(默认启动项目)(.NET Framework 4.7),
  • 包含视图模型和非 UI 内容的核心"项目 - 类库项目 (.NET Standard 2.0)
  • 包含所有实体框架数据层的关系"项目 - 我喜欢将它们分开(.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 解决 DbContextes 就足够了.

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

    但是,它不是 - 因为在我的 WPF 项目上运行 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 go.microsoft/fwlink/?linkid=851728 for additional patterns supported at design time.

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

    推荐答案

    我复制了你的解决方案并找到了...一个解决方案:)

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

  • 核心"项目 - 名为 ClassLibrary1
  • 关系"项目 - 称为 EFClssLibrary
  • WPF 应用项目 - 名为 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 类库.

    依赖关系:

    • 类库1
    • 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 文件中引用该文件(有关更多信息,请查看 本页)

    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.

    我的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="..ClassLibrary1ClassLibrary1.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).

    类型

    PM> Add-Migration Initial

    然后

    PM> Update-Database

    3.WPF 应用项目

    名称:WpfApp1.

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

    依赖关系:

    • 类库1
    • EFClassLibrary
    • Microsoft.EntityFrameworkCore (v2.1.1)

    在这个项目中,我没有添加任何文件.A 刚刚编辑了 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 迁移

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

    发布评论

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

    >www.elefans.com

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