为类库创建实体框架核心迁移

时间:2018-09-07 13:27:49

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

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

PM> Add-Migration Update37

但我总是收到错误消息:

  

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

我的解决方案结构:

enter image description here

如您所见,我有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类和MesDBModels类一起放在DBContext项目中,但是结果是一样的。 我遵循了这个: Using Entity Framework Core migrations for class library project 还有这个 : https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/ 但是,老兄,我误会了某件事或做错了什么。

请帮助。

1 个答案:

答案 0 :(得分:0)

您在输入中有错字

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时,框架会为IDesignTimeDbContextFactory寻找DBContext,但是您要为基础DbContext实现一个工厂。在此代码中将DbContext修改为DBContext,它应该可以工作。