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

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

我们一直试图在.Net Standard 1.6类库中运行EF Core Migration,但是失败了。但是,在.Net Core 1.1类库中也是如此。

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.

.NET STANDARD是否支持EF迁移?

Is EF Migration supported in .NET STANDARD?

推荐答案

没关系,但是有一个复杂性,我想解释一下。

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

案例:Asp.Net核心,我有一个包含Db-Context的标准库。我想在这种情况下迁移,但是标准库不直接接受迁移,需要一个启动项目。

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

我们将从程序包管理器控制台下拉菜单中选择Caller Project,但它将创建库项目中的迁移文件。 之后,不要从下拉菜单中选择其他项目,而直接从调用者项目中运行不带任何参数。

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

尝试奇怪但正确

更新: 我已经将我的项目Asp EntityFrameworkCore 3.1.3升级到了3.1.4,尚无法正常工作并给出错误:

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 :

EntityFramework软件包未安装在项目上

The EntityFramework package is not installed on project

,并且出现此错误

无法创建类型为 DomainDbContext的对象。对于在设计时支持的不同模式

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

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

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 Add迁移?

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

发布评论

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

>www.elefans.com

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