我正在使用 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”参数。”你能帮我做对吗?谢谢!
3 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
看起来有几个类继承自 DbContext 类(可能来自某些 NuGet 包)。所以添加迁移
Add-Migration MyMigration -context DataContextName
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
dotnet ef migrations add <your_migration_name> -c <your_context_class_name>
[--上下文 | -C]
要使用的 DbContext 类。仅类名或完全限定名称空间。如果省略此选项,EF Core 将查找上下文类。如果有多个上下文类,则需要此选项。
来自https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#common-options
- 3 回答
- 0 关注
- 496 浏览
添加回答
举报
0/150
提交
取消