C# 不同项目中模型的EF迁移
每次我在package manager控制台中运行命令C# 不同项目中模型的EF迁移,c#,entity-framework,asp.net-core,migration,C#,Entity Framework,Asp.net Core,Migration,每次我在package manager控制台中运行命令Add Migration-Context ApplicationDbContext,迁移似乎都试图创建一个已经存在于另一个DbContext中的表 我有以下项目结构: 我想要创建的表是CheckoutToken模型: public class CheckoutToken { [Key] public string Token { get; set; } public string UserId { get; set;
Add Migration-Context ApplicationDbContext
,迁移似乎都试图创建一个已经存在于另一个DbContext中的表
我有以下项目结构:
我想要创建的表是CheckoutToken
模型:
public class CheckoutToken
{
[Key]
public string Token { get; set; }
public string UserId { get; set; }
public int LicenseId { get; set; }
public DateTime Created { get; set; }
public DateTime Expires { get; set; }
public License License { get; set; }
public virtual ApplicationUser User { get; set; }
}
ApplicationUser
模型是一个存在的表,这是一个模型:
public class ApplicationUser : IdentityUser
{
public bool SmsTwoFactorEnabled { get; set; }
}
当我运行Add Migration命令时,我得到了一个迁移,看起来它试图在Up()
方法中创建一个名为“ApplicationUser”的表,如下所示:
migrationBuilder.CreateTable(
name: "ApplicationUser",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(nullable: true),
NormalizedUserName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true),
NormalizedEmail = table.Column<string>(nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
SmsTwoFactorEnabled = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ApplicationUser", x => x.Id);
});
有人知道我做错了什么吗?由于应用程序中有两个DB上下文(IdentityDbContext和ApplicationDbContext),可能它们使用不同的数据库来存储表,所以我已经困惑了好几天了。您可以检查Startup.cs或IdentityHostingStartup.cs文件中的ConfigureServices方法,确保它们使用相同的连接字符串
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("IndentityDbContextConnection")));
services.AddDbContext<IndentityDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("IndentityDbContextConnection")));
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“IndentityDbContextConnection”);
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“IndentityDbContextConnection”);
此外,对于ApplicationDbContext,请尝试按如下方式修改代码:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public类ApplicationDbContext:IdentityDbContext
{
然后,启用迁移以检查结果
编辑:
如果仍然不起作用,请尝试删除Up()和Down()方法中不必要的语句
此外,如果您遇到新属性not add to the AspNetUsers问题,请尝试在Up()/Down()方法中添加以下代码,例如(使用以下代码在AspNetUsers表中添加地址列,然后使用迁移更新数据库):
protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.AddColumn(
姓名:“地址”,
表:“AspNetUsers”,
可为空:false,
默认值:0);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropColumn(
姓名:“地址”,
表:“AspNetUsers”);
}
由于应用程序中有两个DB上下文(IdentityDbContext和ApplicationDbContext),可能它们使用不同的数据库来存储表。您可以检查Startup.cs或IdentityHostingStartup.cs文件中的ConfigureServices方法,确保它们使用相同的连接字符串
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("IndentityDbContextConnection")));
services.AddDbContext<IndentityDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("IndentityDbContextConnection")));
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“IndentityDbContextConnection”);
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“IndentityDbContextConnection”);
此外,对于ApplicationDbContext,请尝试按如下方式修改代码:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public类ApplicationDbContext:IdentityDbContext
{
然后,启用迁移以检查结果
编辑:
如果仍然不起作用,请尝试删除Up()和Down()方法中不必要的语句
此外,如果您遇到新属性not add to the AspNetUsers问题,请尝试在Up()/Down()方法中添加以下代码,例如(使用以下代码在AspNetUsers表中添加地址列,然后使用迁移更新数据库):
protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.AddColumn(
姓名:“地址”,
表:“AspNetUsers”,
可为空:false,
默认值:0);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropColumn(
姓名:“地址”,
表:“AspNetUsers”);
}
虽然我是从.NET CoreIdentityDbContext
派生的,但当我创建迁移时,它看起来像是在尝试创建所有的标识表,即AspNetUsers、AspNetRoles等。我还检查了Startup.cs
文件,并检查了两个上下文是否使用相同的连接字符串,这是真的@KTOV,尝试删除Up()和Down()中不必要的语句方法。虽然我在创建迁移时从.NET CoreIdentityDbContext
派生,但它似乎在尝试创建所有标识表,如AspNetUsers、AspNetRoles等。我还检查了Startup.cs
文件,并检查了两个上下文是否使用相同的连接字符串,这是真的@KTOV,尝试删除Up()和Down()方法中不必要的语句。