.NET Core Entity Framework - 在类库中为上下文添加迁移 [英] .NET Core Entity Framework - Add migration for Context in class library

查看:24
本文介绍了.NET Core Entity Framework - 在类库中为上下文添加迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向 .NET Core 类库中的实体框架数据库上下文添加初始迁移时遇到问题.

当我跑步时:

dotnet ef migrations add migrationName -c PlaceholderContext

出现错误:

无法在启动项目Placeholder.Data"上调用此命令.此版本的 Entity Framework Core .NET 命令行工具不支持 ASP.NET Core 和 .NET Core 应用程序中的类库项目上的命令.有关详细信息和解决方法,请参阅 http://go.microsoft.com/fwlink/?LinkId=798221.

所以我点击了

Placeholder.Web => Startup.cs

 public void ConfigureServices(IServiceCollection services){//添加框架服务.services.AddApplicationInsightsTelemetry(Configuration);服务.AddMvc();//这里我们注册DB上下文,(业务层中的静态类)services.InjectBusinessContext(@"Data Source=(localdb)ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;");services.InjectWebServices();services.InjectBusinessServices();}

我怎样才能克服这个非常烦人的问题?

更新 (1)

我已将我的 Placeholder.Data 类库转换为具有静态 main 方法的应用程序".因为我不能再从 Placeholder.Business 引用 Placeholder.Data 我必须使用微软文档页面上列出的解决方法 2.当我现在运行迁移脚本时,我得到以下信息:

<块引用>

没有为此 DbContext 配置数据库提供程序.一种可以通过覆盖 DbContext.OnConfiguring 来配置提供程序方法或通过在应用程序服务提供者上使用 AddDbContext.如果使用 AddDbContext,则还要确保您的 DbContext 类型接受一个其构造函数中的 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数

当然这行不通,dbcontext 是从我的 Placeholder.Web 应用程序注册的(通过业务层).然后我唯一的选择是在新的静态 main 方法中添加一个新的上下文,我真的不想这样做..

解决方案

您无需将数据项目转换"为应用程序.这是一个具有类似结构的测试应用:

在Data项目的project.json中,添加asp.net core nuget包.

现在,要创建迁移,只需右键单击数据项目,选择在文件资源管理器中打开文件夹",然后在文件资源管理器中,按 Shift + 右键单击​​并在此处打开命令窗口".

要创建迁移,只需将启动项目"指定为 Web 应用程序(startup.cs 所在的位置)

dotnet ef --startup-project ../TestPatterns2.Web migrations add Second

瞧,迁移:

要将迁移文件夹添加到数据项目中:定义服务时,像这样添加迁移点

services.AddDbContext(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("TestPatterns2.Data")));

I'm having problems adding an initial migration to my Entity Framework database context inside a .NET Core class library.

When I run:

dotnet ef migrations add migrationName -c PlaceholderContext

I get error:

Could not invoke this command on the startup project 'Placeholder.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.

So I clicked on the link and learned that it's not possible to add a migration to a class library. You can however convert the class library project into an "app" project but by doing so I can not reference this "app" project from my business layer (class library).

Project structure:

Placeholder.Web (WebAPI) => Placeholder.Business (class library) => Placeholder.Data (class library)

Placeholder.Web => Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();

        //HERE WE REGISTER DB CONTEXT, (STATIC CLASS IN BUSINESS LAYER)
        services.InjectBusinessContext(@"Data Source=(localdb)ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;");

        services.InjectWebServices();
        services.InjectBusinessServices();
    }

How can I overcome this really annoying problem?

Update (1)

I have converted my Placeholder.Data class library to an "app" with a static main method. Because I can no longer reference Placeholder.Data from Placeholder.Business I have to go with workaround 2 listed on the microsoft doc page. When I now run the migration script I get the following:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext

Doh ofcourse this wont work, the dbcontext is registered from my Placeholder.Web app, (through business layer). Then my only option is to add a new context in the new static main method and I dont really want to do this..

解决方案

You don't need to 'convert' your data project to an app. Here is a test app with a similar structure:

In the project.json in the Data project, add the asp.net core nuget packages.

Now, to create a migration, just right click on the Data project, choose 'Open folder in file explorer,' then in file explorer, press Shift + right click and 'Open command window here.'

To create the migration, just specify the 'startup project' as the web app (where the startup.cs exists)

dotnet ef --startup-project ../TestPatterns2.Web migrations add Second

And voila, migration:

TO ADD THE MIGRATION FOLDER INTO THE DATA PROJECT: when you defined the service, add the migration point like so

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("TestPatterns2.Data")));

这篇关于.NET Core Entity Framework - 在类库中为上下文添加迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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