找到了多个 DbContext

Iba*_*408 17 c# dbcontext entity-framework-core asp.net-core

我正在使用 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

这在我的“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);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

你能帮我把这个做对吗​​?谢谢!

max*_*986 19

看起来有几个类是从 DbContext 类继承而来的(可能来自某个 NuGet 包)。所以添加迁移

Add-Migration MyMigration -context DataContextName
Run Code Online (Sandbox Code Playgroud)

  • 使用“dotnet ef 迁移添加 InitialCreate --context DataContext” (3认同)

Dal*_*man 8

请遵循此语法

Add-Migration [-Name] <String> [-OutputDir <String>] [-Context <String>] [-Project <String>] [-StartupProject <String>] [-Environment <String>] [<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)

在你的情况下,

add-migration MyMigration -Context DataContext
Run Code Online (Sandbox Code Playgroud)


the*_*eld 6

dotnet ef migrations add <your_migration_name> -c <your_context_class_name>
Run Code Online (Sandbox Code Playgroud)

[--上下文| -C]

要使用的 DbContext 类。仅类名或使用命名空间完全限定。如果省略此选项,EF Core 将查找上下文类。如果有多个上下文类,则需要此选项。

来自https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#common-options


ios*_*ser 6

update-database -Context YourContext
Run Code Online (Sandbox Code Playgroud)

  • 您可能想扩展您的答案并解释该命令如何解决问题 (3认同)