Asp.net core mvc 将IdentityUser.Id类型更改为Guid会导致迁移失败
我已将Asp.net core mvc 将IdentityUser.Id类型更改为Guid会导致迁移失败,asp.net-core-mvc,entity-framework-core,.net-core,entity-framework-migrations,asp.net-core-identity,Asp.net Core Mvc,Entity Framework Core,.net Core,Entity Framework Migrations,Asp.net Core Identity,我已将IdentityUser.Id属性的类型从string更改为Guid,并添加了一个与基类LogEntry的关系,该基类具有派生类JournalEntry和权重条目 当我尝试更新数据库时,出现以下错误: System.Data.SqlClient.SqlException:列“AspNetUsers.Id”与外键“FK\u LogEntries\u AspNetUsers\u ApplicationSerid”中引用列“LogEntries.ApplicationSerid”的数据类型不同。
IdentityUser.Id
属性的类型从string
更改为Guid
,并添加了一个与基类LogEntry
的关系,该基类具有派生类JournalEntry
和权重条目
当我尝试更新数据库时,出现以下错误:
System.Data.SqlClient.SqlException:列“AspNetUsers.Id”与外键“FK\u LogEntries\u AspNetUsers\u ApplicationSerid”中引用列“LogEntries.ApplicationSerid”的数据类型不同。无法创建约束或索引。请参阅前面的错误
实体框架应使用uniqueidentifier
类型的Id
字段创建AspNetUsers
表,但它不是。它是用nvarchar(450)
创建的
我已将相关源代码包括在下面:
ApplicationUser.cs
public class ApplicationUser : IdentityUser<Guid>
{
public ApplicationUser()
: base()
{
Id = Guid.NewGuid();
}
public virtual ICollection<LogEntry> LogEntries { get; set; }
}
public abstract class LogEntry
{
public virtual Guid Id { get; set; }
public virtual DateTime CreatedDt { get; set; }
public virtual Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class JournalEntry : LogEntry
{
public string Text { get; set; }
}
public class WeightEntry : LogEntry
{
public short Kilograms { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
DbSet<JournalEntry> JournalEntries { get; set; }
DbSet<WeightEntry> WeightEntries { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(x =>
{
x.HasMany(y => y.LogEntries)
.WithOne(y => y.ApplicationUser)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<LogEntry>(x =>
{
x.ToTable("LogEntries");
x.HasOne(y => y.ApplicationUser)
.WithMany(y => y.LogEntries)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<JournalEntry>(x =>
{
x.ToTable("JournalEntries");
});
builder.Entity<WeightEntry>(x =>
{
x.ToTable("WeightEntries");
});
}
}
JournalEntry.cs
public class ApplicationUser : IdentityUser<Guid>
{
public ApplicationUser()
: base()
{
Id = Guid.NewGuid();
}
public virtual ICollection<LogEntry> LogEntries { get; set; }
}
public abstract class LogEntry
{
public virtual Guid Id { get; set; }
public virtual DateTime CreatedDt { get; set; }
public virtual Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class JournalEntry : LogEntry
{
public string Text { get; set; }
}
public class WeightEntry : LogEntry
{
public short Kilograms { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
DbSet<JournalEntry> JournalEntries { get; set; }
DbSet<WeightEntry> WeightEntries { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(x =>
{
x.HasMany(y => y.LogEntries)
.WithOne(y => y.ApplicationUser)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<LogEntry>(x =>
{
x.ToTable("LogEntries");
x.HasOne(y => y.ApplicationUser)
.WithMany(y => y.LogEntries)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<JournalEntry>(x =>
{
x.ToTable("JournalEntries");
});
builder.Entity<WeightEntry>(x =>
{
x.ToTable("WeightEntries");
});
}
}
JournalEntry.cs
public class ApplicationUser : IdentityUser<Guid>
{
public ApplicationUser()
: base()
{
Id = Guid.NewGuid();
}
public virtual ICollection<LogEntry> LogEntries { get; set; }
}
public abstract class LogEntry
{
public virtual Guid Id { get; set; }
public virtual DateTime CreatedDt { get; set; }
public virtual Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class JournalEntry : LogEntry
{
public string Text { get; set; }
}
public class WeightEntry : LogEntry
{
public short Kilograms { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
DbSet<JournalEntry> JournalEntries { get; set; }
DbSet<WeightEntry> WeightEntries { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(x =>
{
x.HasMany(y => y.LogEntries)
.WithOne(y => y.ApplicationUser)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<LogEntry>(x =>
{
x.ToTable("LogEntries");
x.HasOne(y => y.ApplicationUser)
.WithMany(y => y.LogEntries)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<JournalEntry>(x =>
{
x.ToTable("JournalEntries");
});
builder.Entity<WeightEntry>(x =>
{
x.ToTable("WeightEntries");
});
}
}
ApplicationDbContext.cs
public class ApplicationUser : IdentityUser<Guid>
{
public ApplicationUser()
: base()
{
Id = Guid.NewGuid();
}
public virtual ICollection<LogEntry> LogEntries { get; set; }
}
public abstract class LogEntry
{
public virtual Guid Id { get; set; }
public virtual DateTime CreatedDt { get; set; }
public virtual Guid ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class JournalEntry : LogEntry
{
public string Text { get; set; }
}
public class WeightEntry : LogEntry
{
public short Kilograms { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
DbSet<JournalEntry> JournalEntries { get; set; }
DbSet<WeightEntry> WeightEntries { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(x =>
{
x.HasMany(y => y.LogEntries)
.WithOne(y => y.ApplicationUser)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<LogEntry>(x =>
{
x.ToTable("LogEntries");
x.HasOne(y => y.ApplicationUser)
.WithMany(y => y.LogEntries)
.HasForeignKey(y => y.ApplicationUserId)
.HasPrincipalKey(y => y.Id)
.IsRequired();
});
builder.Entity<JournalEntry>(x =>
{
x.ToTable("JournalEntries");
});
builder.Entity<WeightEntry>(x =>
{
x.ToTable("WeightEntries");
});
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
DbSet日志{get;set;}
DbSet WeightEntries{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity(x=>
{
x、 HasMany(y=>y.LogEntries)
.WithOne(y=>y.ApplicationUser)
.HasForeignKey(y=>y.applicationserid)
.HasPrincipalKey(y=>y.Id)
.IsRequired();
});
builder.Entity(x=>
{
x、 ToTable(“日志条目”);
x、 HasOne(y=>y.ApplicationUser)
.具有多个(y=>y.LogEntries)
.HasForeignKey(y=>y.applicationserid)
.HasPrincipalKey(y=>y.Id)
.IsRequired();
});
builder.Entity(x=>
{
x、 ToTable(“日志”);
});
builder.Entity(x=>
{
x、 ToTable(“权重条目”);
});
}
}
Startup.csConfigureServices
services.AddIdentity<ApplicationUser, IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationDbContext, Guid>()
.AddDefaultTokenProviders();
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();