我正在尝试实现 SimpleMembership 实体,以便可以控制它的某些方面,这是我创建的类:
[Table("webpages_Roles")]
public class Role
{
public Role()
{
UsersInRoles = new List<UsersInRole>();
}
[Key]
public int RoleId { get; set; }
[StringLength(256)]
public string RoleName { get; set; }
//public ICollection<Membership> Members { get; set; }
[ForeignKey("RoleId")]
public ICollection<UsersInRole> UsersInRoles { get; set; }
}
[Table("webpages_UsersInRoles")]
public class UsersInRole
{
[Key, Column(Order = 0)]
public int RoleId { get; set; }
[Key, Column(Order = 1)]
public int UserId { get; set; }
[Column("RoleId"), InverseProperty("UsersInRoles")]
public Role Roles { get; set; }
[Column("UserId"), InverseProperty("UsersInRoles")]
public Membership Members { get; set; }
}
[Table("webpages_Membership")]
public class Membership
{
public Membership()
{
//Roles = new List<Role>();
OAuthMemberships = new List<OAuthMembership>();
UsersInRoles = new List<UsersInRole>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
[StringLength(128)]
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
[Required, StringLength(128)]
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
[Required, StringLength(128)]
public string PasswordSalt { get; set; }
[StringLength(128)]
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
//public ICollection<Role> Roles { get; set; }
[ForeignKey("UserId")]
public ICollection<OAuthMembership> OAuthMemberships { get; set; }
[ForeignKey("UserId")]
public ICollection<UsersInRole> UsersInRoles { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public bool Status { get; set; }
public string EmailId { get; set; }
}
现在,只要我尝试插入新用户,它就会正确插入到 userProfile 和 pages_Membership 表中,但是一旦应用程序尝试为用户分配角色,就会引发异常
*INSERT 语句与 FOREIGN KEY 约束“FK_dbo.webpages_UsersInRoles_dbo.webpages_Membership_UserId”冲突。冲突发生在数据库“PCSPI”、表“dbo.webpages_Membership”、列“UserId”中。该语句已终止。*
我尝试使用 sql server 手动插入它并插入,但是一旦我尝试在我的应用程序上执行此操作,就会引发异常。
我究竟做错了什么?模型定义不明确吗?提前致谢