简体   繁体   English

将迁移添加到我的类库 ef core

[英]Add migration to my class library ef core

I have a project with just one class library.我有一个只有一个类库的项目。 I'm now trying to generate a migration in the class library.我现在正在尝试在类库中生成迁移。

Herer is my DbContext :这是我的DbContext

public class DatabaseContext : DbContext, IDatabaseContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) 
    { }

    public DbSet<Security> Services { get; set; }
    public DatabaseFacade DatabaseAccessor => this.Database;

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.EnableSensitiveDataLogging();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("dbo");

        modelBuilder.Entity<Security>().HasNoKey();

        base.OnModelCreating(modelBuilder);
    }
}

When I run the command当我运行命令时

dotnet ef migrations add InitialCreate --project SecurityAttribute

I get the following error:我收到以下错误:

Unable to create an object of type 'DatabaseContext'.无法创建类型为“DatabaseContext”的对象。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

My IServiceCollections looks like this:我的IServiceCollections如下所示:

public static IServiceCollection AddServiceAndRoleSecurity(this IServiceCollection services)
{
    services.AddTransient<ISecurityService, SecurityService>();
    services.AddMemoryCache();

    services.AddTransient<ISecurityRepository, SecurityRepository>();
    services.AddDbContext<IDatabaseContext, DatabaseContext>(options => options.UseSqlServer(
                 @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SecurityAttribute;Integrated Security=SSPI;",
                sqlServerOptions => sqlServerOptions.CommandTimeout(10 * 60)
             ));
    services.AddTransient<IDatabaseContextFactory, DatabaseContextFactory>();
    return services;
}

public static IHost CreateDatabase(this IHost host)
{
    using (var scope = host.Services.CreateScope())
    {
        var db = scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
        var migrations = db.DatabaseAccessor.GetPendingMigrations();

        if (migrations.Any())
            db.DatabaseAccessor.Migrate();
    }
    return host;
}

My class library will be used as a nuget package.我的类库将用作 nuget 包。 That's why I only have a class library.这就是为什么我只有一个类库。

I think the best solution is to create a test project with Startup.cs.我认为最好的解决方案是使用 Startup.cs 创建一个测试项目。 Where in configure services you register your DbContext and use this project for managing migrations.在配置服务的何处注册DbContext并使用此项目来管理迁移。

To specify where migrations should be added you can add this option to DbContext options:要指定应在何处添加迁移,您可以将此选项添加到DbContext选项:


options.UseSqlServer(
    connectionString,
    x => x.MigrationsAssembly("MyLib.Migrations"));

where MyLib.Migrations - name of the assembly where to store migrations where MyLib.Migrations - 存储迁移的程序集名称

Are you running your command (dotnet ef migrations add InitialCreate --project SecurityAttribute) in the package manager console?您是否在包管理器控制台中运行命令(dotnet ef migrations add InitialCreate --project SecurityAttribute)? If so do you have the default project set correctly in the package manager console drop down?如果是这样,您是否在包管理器控制台下拉列表中正确设置了默认项目? Is the start up project for the solution set to be the one with the Startup.cs?该解决方案的启动项目是否与 Startup.cs 相同?

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

相关问题 .NET 标准库是否支持 EF Core 添加迁移? - Is EF Core Add Migration Supported from .NET Standard Library? 在 PackageManagerConsole 中添加迁移时,EF Core IEntityTypeConfiguration class 中未命中断点 - Breakpoint not hit in EF Core IEntityTypeConfiguration class on add-migration in PackageManagerConsole EF Core 添加迁移调试 - EF Core Add Migration Debugging 如何将EF核心迁移添加到.net核心2类库? - How to add EF core migrations to .net core 2 class library? .NET核心实体框架 - 在类库中为Context添加迁移 - .NET Core Entity Framework - Add migration for Context in class library EF 核心 - 为没有连接字符串和另一个类库中的数据库创建迁移 - EF core - Creating a migration for a database without a connection string and in another class library 在类库中执行asp.net核心EF迁移而无需对连接字符串进行硬编码 - Executing asp.net core EF migration in a class library without hardcoding connection string EF Core 迁移错误:PositionalParameterNotFound,Add-Migration - EF Core migration error: PositionalParameterNotFound,Add-Migration 在EF中的模型类上添加具有Required属性的迁移 - Add migration with Required attribute on model class in EF EF Core 1.1到WebApi核心 - 添加迁移失败 - EF Core 1.1 to WebApi Core - Add-Migration fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM