找到多个 DbContext

我正在使用 AspCore 2 实现代码优先数据库。我有一个“DataContext.cs”,如下所示:


public class ApplicationUser : IdentityUser

{

    public string FirstName { get; set; }

    public string MiddelName { get; set; }

    public string LastName { get; set; }

    public bool IsActive { get; set; }

    public DateTime? DateAdded { get; set; }

}


public class DataContext : IdentityDbContext<ApplicationUser>

{

    public DataContext(DbContextOptions<DataContext> options) : base(options) {}


protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

             base.OnModelCreating(modelBuilder);


          //AspNetUsers -> User

        modelBuilder.Entity<ApplicationUser>()

            .ToTable("User");

        //AspNetRoles -> Role

        modelBuilder.Entity<IdentityRole>()

            .ToTable("Role");

        //AspNetUserRoles -> UserRole

        modelBuilder.Entity<IdentityUserRole>()

            .ToTable("UserRole");

        //AspNetUserClaims -> UserClaim

        modelBuilder.Entity<IdentityUserClaim>()

            .ToTable("UserClaim");

        //AspNetUserLogins -> UserLogin

        modelBuilder.Entity<IdentityUserLogin>()

            .ToTable("UserLogin");

    }

}

这在我的“startup.cs”中


public class Startup

{

    public Startup(IConfiguration configuration)

    {

        Configuration = configuration;

    }


    public IConfiguration Configuration { get; }


    // This method gets called by the runtime. Use this method to add services to the container.

    public void ConfigureServices(IServiceCollection services)

    {

        services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    }


  

当我尝试运行 dotnet 迁移时,dotnet ef migrations add InitialCreate出现以下错误:


“找到了多个 DbContext。指定要使用的 DbContext。对 PowerShell 命令使用“-Context”参数,对 dotnet 命令使用“--context”参数。”


你能帮我做对吗?谢谢!


倚天杖
浏览 499回答 3
3回答

叮当猫咪

看起来有几个类继承自 DbContext 类(可能来自某些 NuGet 包)。所以添加迁移Add-Migration&nbsp;MyMigration&nbsp;-context&nbsp;DataContextName

隔江千里

请遵循此语法Add-Migration&nbsp;[-Name]&nbsp;<String>&nbsp;[-OutputDir&nbsp;<String>]&nbsp;[-Context&nbsp;<String>]&nbsp;[-Project&nbsp;<String>]&nbsp;[-StartupProject&nbsp;<String>]&nbsp;[-Environment&nbsp;<String>]&nbsp;[<CommonParameters>]在你的情况下,add-migration&nbsp;MyMigration&nbsp;-Context&nbsp;DataContext

一只萌萌小番薯

dotnet&nbsp;ef&nbsp;migrations&nbsp;add&nbsp;<your_migration_name>&nbsp;-c&nbsp;<your_context_class_name>[--上下文 |&nbsp;-C]要使用的 DbContext 类。仅类名或完全限定名称空间。如果省略此选项,EF Core 将查找上下文类。如果有多个上下文类,则需要此选项。来自https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#common-options
打开App,查看更多内容
随时随地看视频慕课网APP