Simplemembership webpages_UsersInRoles代码首先出现FK错误

时间:2013-04-25 21:41:59

标签: c# asp.net asp.net-mvc entity-framework

我正在尝试实现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和webpages_Membership上,但是一旦应用程序尝试为用户分配角色,就会抛出异常

* INSERT语句与FOREIGN KEY约束“FK_dbo.webpages_UsersInRoles_dbo.webpages_Membership_UserId”冲突。冲突发生在数据库“PCSPI”,表“dbo.webpages_Membership”,列“UserId”中。 声明已经终止。*

我尝试使用sql server手动插入它并插入但是只要我尝试在我的应用程序上执行它就会抛出异常。

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

1 个答案:

答案 0 :(得分:0)

你不是在阅读评论。我认为您从http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model

获得了该架构

下面的评论之一说,架构不正确,并提供正确的架构,defo在我使用它时起作用。我不想在此重复。转到上述网站并再次检查。 (Senad Karic的评论)

P.S。别担心,我第一次使用它时也做了同样的事情:)