ASP标识表列更改,但新列名称无法进行映射
我已经更改了asp Identity,因此在AspNetIdentity
表的数据库Id
列中是UserId
:
modelBuilder.Entity() .Property(p => p.Id) .HasColumnName("UserId");
这很好。 生成的表具有UserId
而不是Id
。
现在我有另一个表,应该用UserId
与AspNetUsers
映射:
这样写时:
public class JobApply { ... public int UserId { get; set; } public virtual ApplicationUser ApplicationUser { get; set; }
数据库中的表格如下:
如果我这样写:
public class JobApply { ... public int ApplicationUserId { get; set; } public virtual ApplicationUser ApplicationUser { get; set; }
数据库看起来像:
第一个选项创建了UserId
字段,但它不是FK
而是将新字段添加为FK
。
我如何正确映射这个以使表JobApply
与字段UserId
成为FK
到AspNetUsers
?
UPDATE
当我添加这个:
base.OnModelCreating(modelBuilder); modelBuilder.Entity() .Property(p => p.Id) .HasColumnName("UserId"); modelBuilder.Entity() .HasRequired(e => e.ApplicationUser) .WithMany() .HasForeignKey(e => e.UserId);
表关系如下所示:
这是两个类:
public class ApplicationUser : IdentityUser { public string Email { get; set; } public string ConfirmationToken { get; set; } public bool IsConfirmed { get; set; } public string PasswordResetToken { get; set; } public int CityId { get; set; } public virtual City City { get; set; } public virtual ICollection JobApplies { get; set; } } public class JobApply { public int JobApplyId { get; set; } public int UserId { get; set; } public int JobId { get; set; } public int JobApplyStatusId { get; set; } public DateTime CreatedDate { get; set; } public virtual ApplicationUser ApplicationUser { get; set; } public virtual Job Job { get; set; } public virtual JobApplyStatus JobApplyStatus { get; set; } }
使用流畅的API定义您的关系,如下所示:
modelBuilder.Entity() .HasRequired(e => e.ApplicationUser) .WithMany(e => e.JobApplies) .HasForeignKey(e => e.UserId);
您也可以从ApplicationUser
类中删除属性UserId
,然后像这样定义映射:
modelBuilder.Entity() .HasRequired(e => e.ApplicationUser) .WithMany(e => e.JobApplies) .Map(m => m.MapKey("UserId"));
- 交换Azure Web App部署槽会注销ASP.NET Core RC2中的所有用户
- 错误:“Id字段是必需的。”在使用AspNet.Identity 2.0在数据库中注册新用户时
- ASP.Net Identity如何设置目标DB?
- 如何删除dbo.AspNetUserClaims和dbo.AspNetUserLogins表(IdentityUserClaim和IdentityUserLogin实体)?
- 在覆盖成员时注册了抛出的inheritance安全规则
- 充分利用具有n(3)层架构的MVC Owin身份
- 使用带有IdentityFactory中间件的IOC容器
- 我如何创建UserManager的实例
- 如何设置asp.net标识cookie到期时间