1

我正在尝试实现 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 手动插入它并插入,但是一旦我尝试在我的应用程序上执行此操作,就会引发异常。

我究竟做错了什么?模型定义不明确吗?提前致谢

4

1 回答 1

0

你不是在看评论。我认为您从以下位置获得了该架构:http: //blog.spontaneouspublicity.com/includes-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model

下面的评论之一说架构不正确,并给出了正确的架构,在我使用它时,defo 可以正常工作。我不想在这里重复。转到上述站点并再次检查。(Senad Karic 评论)

PS别担心,我第一次使用时也是这样做的:)

于 2013-04-27T17:35:46.443 回答