【问题标题】:Create Entity Framework Core migration for class library为类库创建 Entity Framework Core 迁移
【发布时间】:2019-02-12 20:50:10
【问题描述】:

我正在尝试在 EF Core 类库中生成迁移,但到目前为止无济于事。我正在使用包管理器控制台(Visual Studio 2017 社区)编写这样的命令:

PM> Add-Migration Update37

但我总是收到错误:

无法创建“DBContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或查看https://go.microsoft.com/fwlink/?linkid=851728 了解设计时支持的其他模式。

我的解决方案结构:

如您所见,我有 2 个项目,其中 MesDBModels 是我想为其生成迁移到 Migrations 文件夹的类库。我添加了一个DummyProject,以便在.NETcore 运行时提供“启动项目”。 DummyProject 还包含 IDesignTimeDBContextFactory<> 接口在 DBContextFactory 类中的实现。 DummyProject 还包含对 MesDBModels 项目的引用。

MesDBModels.Database.DBContext :

using MesDBModels.FluentConfig;
using MesDBModels.Models;
using Microsoft.EntityFrameworkCore;

namespace MesDBModels.Database
{
public class DBContext : DbContext
{
    public DBContext(DbContextOptions<DBContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new AccessCardConfig());
        modelBuilder.ApplyConfiguration(new AlarmConfig());
        modelBuilder.ApplyConfiguration(new BitPositionConfig());
        modelBuilder.ApplyConfiguration(new MachineConfig());
        modelBuilder.ApplyConfiguration(new ParameterTemplateConfig());
        modelBuilder.ApplyConfiguration(new ReferenceConfig());
        modelBuilder.ApplyConfiguration(new ReferenceTemplateConfig());
        modelBuilder.ApplyConfiguration(new UserConfig());
        modelBuilder.ApplyConfiguration(new UserGroupConfig());
        modelBuilder.ApplyConfiguration(new UserPermissionConfig());

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<AccessCard> AccessCards { get; set; }
    public DbSet<Alarm> Alarms { get; set; }
    public DbSet<AlarmDefinition> AlarmDefinitions { get; set; }
    public DbSet<AlarmPriority> AlarmPriorities { get; set; }
    public DbSet<BitPosition> BitPositions { get; set; }
    public DbSet<EventLog> EventLogs { get; set; }
    public DbSet<EventType> EventTypes { get; set; }
    public DbSet<Group> Groups { get; set; }
    public DbSet<Machine> Machines { get; set; }
    public DbSet<MachineState> MachineStates { get; set; }
    public DbSet<MachineStateDefinition> MachineStateDefinitions { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<ParameterTemplate> ParameterTemplates { get; set; }
    public DbSet<ReferenceParameterHistory> ReferenceParameterHistory { get; set; }
    public DbSet<ParameterValueLimit> ParameterValueLimits { get; set; }
    public DbSet<Reference> References { get; set; }
    public DbSet<ReferenceParameterType> ReferenceParameterTypes { get; set; }
    public DbSet<ReferenceTemplate> ReferenceTemplates { get; set; }
    public DbSet<Session> Sessions { get; set; }
    public DbSet<Unit> Units { get; set; }
    public DbSet<UnitStatus> UnitStatuses { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<UserGroup> UserGroups { get; set; }
    public DbSet<UserPermission> UsersPermissions { get; set; }
}

}

DummyProject.DBContextFactory:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace MesDBModels
{
public class DBContextFactory : IDesignTimeDbContextFactory<DbContext>
{ 
    public DbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<DbContext>();
        builder.UseSqlServer(
            "Server=(local)\\serverName;Database=dbName;Trusted_Connection=True;MultipleActiveResultSets=true");

        return new DbContext(builder.Options);
    }
}

}

DummyProject.Program 类:

using System;

namespace DummyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

所以我的问题是:我做错了什么?我曾尝试将DBContextFactory 类与DBContext 类一起放入MesDBModels 项目中,但结果是一样的。 我遵循了这个: Using Entity Framework Core migrations for class library project 和这个 : https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/ 但也许我误解了什么或做错了什么。

请帮忙。

【问题讨论】:

  • 我建议您在外面尝试一下,而不是确保您在正确的 (lib) 项目中运行您的命令。只需打开 PowerShell,转到您的 LIB 项目目录并运行 dotnet ef migration add xy 看看会发生什么。 (据我记得,如果你在 pm 控制台上运行它,你必须将你的 lib 项目设置为启动项目)
  • 您是在实施IDesignTimeDbContextFactory&lt;DbContext&gt; 还是IDesignTimeDbContextFactory&lt;DBContext&gt;?这些名称在视觉上非常接近,我不确定这是否是拼写错误,但如果不是,那么很可能是问题的原因。

标签: c# .net-core entity-framework-core entity-framework-migrations


【解决方案1】:

你写错了

public class DBContextFactory : IDesignTimeDbContextFactory<DbContext>
{ 
    public DbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<DbContext>();
        builder.UseSqlServer(
            "Server=(local)\\serverName;Database=dbName;Trusted_Connection=True;MultipleActiveResultSets=true");

        return new DbContext(builder.Options);
    }
}

在尝试构建 DBContext 时,框架会为 DBContext 查找 IDesignTimeDbContextFactory,但您正在为基础 DbContext 实现工厂。将此代码中的DbContext 修改为DBContext,它应该可以工作。

【讨论】:

  • 没错。只是拼写错误...在我将DbContext 更改为DBContext 后生成了迁移脚本。谢谢
猜你喜欢
  • 2017-05-16
  • 2020-06-06
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2017-10-27
  • 2021-04-04
  • 1970-01-01
  • 2021-07-26
相关资源
最近更新 更多