简体   繁体   English

无法使用Entity Framework创建迁移脚本

[英]Cannot create migration script using Entity Framework

(neither of the online resources helped, so I am creating a new thread.) (这两个在线资源都没有帮助,所以我创建了一个新线程。)

I am confused with errors I receive when running the following command to create migration scripts: 我对运行以下命令创建迁移脚本时收到的错误感到困惑:

dotnet ef migrations add InitialCreate -v

I receive the following error when I have parameterless constructor for my dbcontext: 当我的dbcontext有无参数构造函数时,我收到以下错误:

No database provider has been configured for this DbContext. 没有为此DbContext配置数据库提供程序。

and when I remove the parameterless constructor, it complains I should put it back: 当我删除无参数构造函数时,它抱怨我应该把它放回去:

No parameterless constructor defined for this object 没有为此对象定义的无参数构造函数

I define the dbcontext as: 我将dbcontext定义为:

public class ItemContext : DbContext
{
    public ItemContext(DbContextOptions<ItemContext> options) : base(options)
    {

    }

    public DbSet<Item> Tools { set; get; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfiguration(new ToolItemEntityTypeConfiguration());
    }
}

The Service is defined as: 该服务定义为:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddCustomDbContext(Configuration);

    var container = new ContainerBuilder();
    container.Populate(services);
    return new AutofacServiceProvider(container.Build());
}

...

public static class CustomExtensionMethods
{
    public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddDbContext<ItemContext>(options =>
        {
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
                                    sqlServerOptionsAction: sqlOptions =>
                                    {
                                        sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                                        sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
                                    });
            options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
        });
        return services;
    }
}

I think this line of code 我想这行代码

public ItemContext(DbContextOptions<ItemContext> options) : base(options)
{

}

Should change to 应该改为

public ItemContext(DbContextOptions options) : base(options)
{

}

If this doesnt work for you try to register this in your Startup.cs 如果这不起作用,请尝试在Startup.cs中注册它

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 

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

相关问题 “脚本迁移”可以在 .NET Entity Framework Core 中创建数据库吗? - Can "Script-Migration" create the database in .NET Entity Framework Core? 如何使用 Entity Framework Core 运行迁移 SQL 脚本 - How to run migration SQL script using Entity Framework Core 无法使用实体框架使用操作创建控制器 - Cannot create Controller with actions using Entity Framework 为类库创建实体框架核心迁移 - Create Entity Framework Core migration for class library 无法使用实体框架创建控制器 - Cannot create controller with Entity Framework 在迁移时在实体框架的dbset中使用通用时 - When using generic in the dbset of the entity framework at Migration 如何使用 Entity Framework Core 从 sql 脚本创建数据库? - How to create a database from sql script using Entity Framework Core? 无法使用实体框架代码优先使用凭据创建数据库? - Cannot Create Database with credentials using Entity Framework Code-First? Entity Framework Core 中的种子脚本正在破坏我的迁移 - Seed script in Entity Framework Core is breaking my migration 实体框架添加迁移错误:“值不能为空” - Error with Entity Framework add-migration: “value cannot be null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM