简体   繁体   English

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

[英]Create Entity Framework Core migration for class library

I'm trying to generate migration in EF Core class library but to no avail so far. 我正在尝试在EF Core类库中生成迁移,但到目前为止无济于事。 I'm using Package Manager Console (Visual Studio 2017 Community) to write a command like this : 我正在使用程序包管理器控制台(Visual Studio 2017社区)编写如下命令:

PM> Add-Migration Update37

but I always get an error: 但我总是得到一个错误:

Unable to create an object of type 'DBContext'. 无法创建类型为“ DBContext”的对象。 Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. 将“ IDesignTimeDbContextFactory”的实现添加到项目中,或在设计时参见https://go.microsoft.com/fwlink/?linkid=851728&clcid=0x804

My solution structure : 我的解决方案结构:

在此处输入图片说明

As you can see I have 2 projects where MesDBModels is class library for which I would like to generate migration to Migrations folder. 如您所见,我有2个项目,其中MesDBModels是类库,我想为其生成到Migrations文件夹的迁移。 I have added a DummyProject in order to provide "Startup project" on .NETcore runtime. 我添加了一个DummyProject以便在.NETcore运行时上提供“启动项目”。 DummyProject also contains implementations of IDesignTimeDBContextFactory<> interface in DBContextFactory class. DummyProject还包含DBContextFactory类中IDesignTimeDBContextFactory<>接口的DBContextFactory Also DummyProject contains reference to MesDBModels project. 此外, DummyProject还包含对MesDBModels项目的引用。

MesDBModels.Database.DBContext : 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 : 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 class : DummyProject.Program类:

using System;

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

So my question is : what am I doing wrong ? 所以我的问题是:我在做什么错? I have tried to put DBContextFactory class in MesDBModels project along with DBContext class but the result is the same. 我试图把DBContextFactoryMesDBModels项目沿DBContext类,但结果是一样的。 I have followed this : Using Entity Framework Core migrations for class library project and this : https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/ but maby I misunderstood something or done something wrong. 我遵循了这一点: 将Entity Framework Core迁移用于类库项目,以及这: https : //garywoodfine.com/using-ef-core-in-a-separate-class-library-project/,但是老妈我误解了一些东西或做了有问题。

Please help. 请帮忙。

You have made a typo in your 你打错了字

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);
    }
}

When trying to construct your DBContext , the framework looks for IDesignTimeDbContextFactory for DBContext , but you are instead implementing a factory for the base DbContext . 在尝试构造DBContext ,框架会为IDesignTimeDbContextFactory寻找DBContext ,但是您将为基础DbContext实现一个工厂。 Modify DbContext to DBContext in this code and it should work. 在此代码DbContext DBContext修改为DBContext ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET核心实体框架 - 在类库中为Context添加迁移 - .NET Core Entity Framework - Add migration for Context in class library 在运行时在 .Net Core 中创建和应用迁移 - Entity Framework Core - Create and Apply Migration at run time in .Net Core - Entity Framework Core .NETSTANDARD类库中的实体框架核心迁移? - Entity Framework Core Migrations In .NETSTANDARD Class Library? 实体框架核心:创建合并多个DbContexts的迁移(具有多个迁移) - Entity Framework Core: create a migration that merges multiple DbContexts (with multiple migrations) 实体框架核心未创建迁移文件 - Entity framework core doesn't create migration file Entity Framework Core 3,为查询定义 DBSet 但不创建迁移条目 - Entity Framework Core 3, Define DBSet for query but dont create migration entry 无法在类库包中的Entity Framework 7 beta4上运行迁移 - Cannot run migration on Entity Framework 7 beta4 in class library package 实体框架核心2迁移问题 - Entity Framework Core 2 Migration issue Entity Framework Core 迁移问题 - Entity Framework Core migration issue StarSchema - 实体框架核心 - 迁移 - StarSchema - Entity Framework Core - Migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM