【问题标题】:Migration with dotnet ef error: Unable to create an object of type 'MyClass'带有 dotnet ef 错误的迁移:无法创建“MyClass”类型的对象
【发布时间】:2020-09-16 00:19:29
【问题描述】:

我正在尝试使用 dotnet ef migrations add MyMigration 进行迁移,但 shell 返回错误:无法创建“AuthDbContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

这是我的 AuthDbContext.cs 文件:

using Auth.Data.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Auth.Data
{
public class AuthDbContext : IdentityDbContext<User>
{
    private readonly string connStr;

    //public DbSet<User> Users { get; set; }

    public AuthDbContext(DbContextOptions<AuthDbContext> options) : base(options)  //string connStr)
    {
        //this.connStr = connStr;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // string connStr = "";
        // connStr = "";
        // if (!optionsBuilder.IsConfigured)
        // {
        //    optionsBuilder
        //        .EnableSensitiveDataLogging(true)
        //        .UseSqlServer(connStr);
        // }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("auth");
        //modelBuilder.Entity<User>().HasKey(p => new { p.User_id });
        base.OnModelCreating(modelBuilder);
    }
    


 }
}

有人可以帮我理解吗?谢谢。

【问题讨论】:

    标签: c# database-migration


    【解决方案1】:

    你忘了添加这一行

    base.OnConfiguring(optionsBuilder);
    

    EF Core 无法完成上下文实例化。

    【讨论】:

    • 谢谢。你能具体说明在哪里吗?对不起,我对它太陌生了
    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多