.NET 标准库是否支持 EF Core 添加迁移?

编程入门 行业动态 更新时间:2024-10-27 10:29:05
本文介绍了.NET 标准库是否支持 EF Core 添加迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们一直在尝试在 .Net Standard 1.6 类库中运行 EF Core Migration,但一直失败.但在 .Net Core 1.1 类库中同样通过得很好.

.NET STANDARD 是否支持 EF 迁移?

解决方案

好吧,但有一个复杂性,我想给出一个解释.

案例:Asp.Net Core,我有一个包含 Db-Context 的标准库.我想移民这个上下文但是标准库不直接接受迁移,需要一个启动项目.

解决方案:启动项目可以间接创建迁移

Enable-Migrations MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsoleAdd-Migration MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsole

我们将从 Package Manager Console DropDown 中选择 Caller Project,但它会在库项目中创建迁移文件.之后不要从下拉列表中选择其他项目并直接从调用方项目运行更新不带任何参数.

更新数据库

尝试奇怪但真实

更新:我已经将我的项目 Asp EntityFrameworkCore 3.1.3 升级到 3.1.4 并且那些没有正常工作并给出错误:

EntityFramework 包未安装在项目上

还有这个错误

无法创建类型为DomainDbContext"的对象.为了设计时支持不同的模式

这是一个 EntityFramework.Core 应用程序!我还安装了 EntityFramework 到标准库.但它无法理解并重新启动 V.S.这次需要将entityframework安装到启动MVC(Web)项目中.我不能那样做.我决定添加一个新的控制台应用程序来解决这个需求.并为此创建了以下应用程序

install-Package Microsoft.Extensions.Configuration安装包 Microsoft.Extensions.Configuration.Json安装包 Microsoft.Extensions.Configuration.CommandLine安装包 Microsoft.Extensions.Configuration.EnvironmentVariables

class 程序{公共类 DesignTimeDbContextFactory : IDesignTimeDbContextFactory{public DomainDbContext CreateDbContext(string[] args){IConfigurationRoot 配置 = new ConfigurationBuilder().AddJsonFile("appsettings.json", 可选: true, reloadOnChange: true).AddEnvironmentVariables()//.SetBasePath(Directory.GetCurrentDirectory())//.AddJsonFile("appsettings.json").建造();var builder = new DbContextOptionsBuilder();var connectionString = configuration.GetConnectionString("DefaultConnection");builder.UseSqlServer(connectionString);返回新的 DomainDbContext(builder.Options);}}静态无效主(字符串 [] args){Console.WriteLine("Hello World!");}}

这次成功了!再次使用上述命令:) 加油

We have been trying to run EF Core Migration in .Net Standard 1.6 class library, but it has been failing. But same passes very well in .Net Core 1.1 class library.

Is EF Migration supported in .NET STANDARD?

解决方案

All that right but there is a complexity and i would like to give an explanation.

Case: Asp.Net Core, I have a standard library that containing Db-Context. I want to migrate this context but standard library doesn't accepting migration directly, needs a startup project.

Solution: startup project is able to create migration indirectly

Enable-Migrations MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsole Add-Migration MyMigration -Project DB-ContextProjectNameThatIsStandartLib -StartupProject CallerExecutableProjectName_LikeMVCWebProjectOrConsole

We will choose Caller Project from the Package Manager Console DropDown but it will create the migration file in the library project. After that don't choose other project from the drop down and run update from the caller project directly without any argument.

Update-Database

try it strange but true

Update: I have upgraded my project Asp EntityFrameworkCore 3.1.3 to 3.1.4 and those on up hasnt worked firectly and gave an error :

The EntityFramework package is not installed on project

and this error

Unable to create an object of type 'DomainDbContext'. For the different patterns supported at design time

This is an EntityFramework.Core application! I have installed also EntityFramework to the standart library. But it couldnt understand and restarted V.S. This time it needed to install entityframework to starter MVC (Web) project. I can't do that. I decide to add a new Console Application to solving this requirement. And created below application for this

install-Package Microsoft.Extensions.Configuration Install-Package Microsoft.Extensions.Configuration.Json Install-Package Microsoft.Extensions.Configuration.CommandLine Install-Package Microsoft.Extensions.Configuration.EnvironmentVariables

class Program { public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<DomainDbContext> { public DomainDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() //.SetBasePath(Directory.GetCurrentDirectory()) //.AddJsonFile("appsettings.json") .Build(); var builder = new DbContextOptionsBuilder<DomainDbContext>(); var connectionString = configuration.GetConnectionString("DefaultConnection"); builder.UseSqlServer(connectionString); return new DomainDbContext(builder.Options); } } static void Main(string[] args) { Console.WriteLine("Hello World!"); } }

This time it has worked correctly! with the above commands again :) cheer

更多推荐

.NET 标准库是否支持 EF Core 添加迁移?

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

发布评论

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

>www.elefans.com

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