实体框架代码优先关系约束中的从属角色和主要角色中的属性数必须相同

时间:2015-02-24 13:10:28

标签: entity-framework foreign-keys

数据库构建错误:

在模型生成期间检测到一个或多个验证错误:

AccountLinkPermission_AccountLink_Target_AccountLinkPermission_AccountLink_Source ::关系约束中的从属角色和主要角色中的属性数必须相同。

用户类:

public class User
{
    public User()
    {
        AccountLinks = new HashSet<AccountLink>();
    }

    [Key]
    public int UserID { get; set; }

    public virtual ICollection<AccountLink> AccountLinks { get; set; }
}

来宾班:

public class Guest
{
    public Guest()
    {
        AccountLinks = new HashSet<AccountLink>();
    }

    [Key]
    public int GuestID { get; set; }

    public virtual ICollection<AccountLink> AccountLinks { get; set; }
}

AccountLink类:

public class AccountLink
    {
        public AccountLink()
        {
            AccountLinkPermissions = new HashSet<AccountLinkPermission>();
        }

        public AccountLink(int userID, int guestID)
        {
            this.UserID = userID;
            this.GuestID = guestID;
        }

        [Key, Column(Order = 0)]
        public int AccountLinkID { get; set; }
        [Key, Column(Order = 1)]
        public int UserID { get; set; }
        [Key, Column(Order = 2)]
        public int GuestID { get; set; }

        [ForeignKey("UserID")]
        public virtual User User { get; set; }

        [ForeignKey("GuestID")]
        public virtual Guest Guest { get; set; }

        public virtual ICollection<AccountLinkPermission> AccountLinkPermissions { get; set; }
    }

AccountLinkPermission类:

public class AccountLinkPermission
{
    public AccountLinkPermission(int accountLinkID, int permissionID)
    {
        this.AccountLinkID = accountLinkID;
        this.PermissionID = permissionID;
    }

    [Key, Column(Order = 0)]
    public int AccountLinkID { get; set; }
    [Key, Column(Order = 1)]
    public int PermissionID { get; set; }

    [ForeignKey("AccountLinkID")]
    public virtual AccountLink AccountLink { get; set; }

    [ForeignKey("PermissionID")]
    public virtual Permission Permission { get; set; }
}

权限类:

public class Permission
{
    public Permission()
    {
        AccountLinkPermissions = new HashSet<AccountLinkPermission>();
    }

    [Key]
    public int PermissionID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<AccountLinkPermission> AccountLinkPermissions { get; set; }
}

问题:

它不会让我在 AccountLinkPermission ,AccountLinkID和 AccountLink ,AccountLinkID之间加上外键。如果我删除了数据库构建的ForeignKey属性,但在这两个属性之间没有外键。

为什么世界上这不起作用?它让我发疯了。请帮帮我..

2 个答案:

答案 0 :(得分:0)

我认为它与你的数据注释有关。你不用[Key]标记你的外键属性

AccountLink.cs

    [Key, Column(Order = 0)]
    public int AccountLinkID { get; set; }
    [Column(Order = 1)]
    public int UserID { get; set; }
    [Column(Order = 2)]
    public int GuestID { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }

    [ForeignKey("GuestID")]
    public virtual Guest Guest { get; set; }

AccountLinkPermission.cs

[Key, Column(Order = 0)]
public int AccountLinkPermissionID { get; set; }
[Column(Order = 1)]
public int AccountLinkID { get; set; }
[Column(Order = 2)]
public int PermissionID { get; set; }

[ForeignKey("AccountLinkID")]
public virtual AccountLink AccountLink { get; set; }

[ForeignKey("PermissionID")]
public virtual Permission Permission { get; set; }

其他一切看起来都不错。希望有所帮助

答案 1 :(得分:0)

我收到此错误消息的原因是因为AccountLink有三个主键。因为我只是从AccountLinkPermission向AccountLink(AccountLinkID)声明了一个外键,所以它失败了。我不得不为UserID和GuestID设置Foreignkey。

此外,如果您使用链接到同一个表的多个外键,则EF无法按惯例确定哪些导航属性属于一起。我没有使用属性[ForeignKey],而是使用 [InverseProperty] ,它定义了关系另一端的导航属性。

发生了什么变化:

<强> AccountLink:

public class AccountLink
{
    public AccountLink()
    {
        AccountLinkPermissionAccountLinkID = new HashSet<AccountLinkPermission>();
        AccountLinkPermissionUserID = new HashSet<AccountLinkPermission>();
        AccountLinkPermissionGuestID = new HashSet<AccountLinkPermission>();
    }

    public AccountLink(int userID, int guestID)
    {
        AccountLinkPermissionAccountLinkID = new HashSet<AccountLinkPermission>();
        AccountLinkPermissionUserID = new HashSet<AccountLinkPermission>();
        AccountLinkPermissionGuestID = new HashSet<AccountLinkPermission>();
        this.UserID = userID;
        this.GuestID = guestID;
    }

    [Key, Column(Order = 0)]
    public int AccountLinkID { get; set; }
    [Key, Column(Order = 1)]
    public int UserID { get; set; }
    [Key, Column(Order = 2)]
    public int GuestID { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }

    [ForeignKey("GuestID")]
    public virtual Guest Guest { get; set; }

    public virtual ICollection<AccountLinkPermission> AccountLinkPermissionAccountLinkID { get; set; }
    public virtual ICollection<AccountLinkPermission> AccountLinkPermissionUserID { get; set; }
    public virtual ICollection<AccountLinkPermission> AccountLinkPermissionGuestID { get; set; }
}

<强> AccountLinkPermission:

public class AccountLinkPermission
        {
            public AccountLinkPermission(int accountLinkID, int userID, int guestID, int permissionID)
            {
                this.AccountLinkID = accountLinkID;
                this.UserID = userID;
                this.GuestID = guestID;
                this.PermissionID = permissionID;
            }

            [Key, Column(Order = 0)]
            public int AccountLinkID { get; set; }
            [Key, Column(Order = 1)]
            public int UserID { get; set; }
            [Key, Column(Order = 2)]
            public int GuestID { get; set; }
            [Key, Column(Order = 3)]
            public int PermissionID { get; set; }

            [InverseProperty("AccountLinkPermissionAccountLinkID")]
            public virtual AccountLink AccountLink { get; set; }

            [InverseProperty("AccountLinkPermissionUserID")]
            public virtual AccountLink AccountLinkUser { get; set; }

            [InverseProperty("AccountLinkPermissionGuestID")]
            public virtual AccountLink AccountLinkGuest { get; set; }

            [ForeignKey("PermissionID")]
            public virtual Permission Permission { get; set; }
        }