Foregin密钥关系错误

时间:2016-12-14 07:34:01

标签: asp.net-mvc asp.net-mvc-4

我有以下模型类

每个项目在特定语言表中有4种语言

public class Item
    {
        [Key]
        public int Id { get; set; }

    public DateTime? ExpireDate { get; set; }

    }

public class ItemLanguage
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

        [ForeignKey("Language")]
        public int LanguageId { get; set; }

        [ForeignKey("Item")]
        public int ItemId { get; set; }

        public Language Language { get; set; }
        public Item Item { get; set; }

    }

每个项目库在特定语言表中有4种语言

public class ItemLibrary
    {
        [Key]
        public int Id { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime? ExpireDate { get; set; }

    }

public class ItemLibraryLanguage
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [ForeignKey("Language")]
        public int LanguageId { get; set; }

        [ForeignKey("ItemLibrary")]
        public int ItemLibraryId { get; set; }

        public ItemLibrary ItemLibrary { get; set; }
        public Language Language { get; set; }
    }

现在我想将项目添加到ItemLibraryItems表中。 它需要在此表中保存ItemId和ItemLibraryId(不是languageId)。显示库的选定项目时,需要根据登录时选择的语言显示。

public class ItemLibraryItem
    {
        [Key]
        public int Id { get; set; }


        [ForeignKey("ItemLibrary")]
        public int ItemLibraryId { get; set; }

        [ForeignKey("Item")]
        public int ItemId { get; set; }

        public ItemLibrary ItemLibrary { get; set; }

        public Item Item { get; set; }
    }

但是当我尝试使用ItemLibraryItem作为模型类来创建Controller时,它会给出错误 实体类型'ItemLibraryItem'和'ItemLibraryLanguage'不能共享表'ItemLibraryLanguages',因为它们不在同一类型层次结构中,或者没有有效的一对一外键关系,它们之间具有匹配的主键。

0 个答案:

没有答案
相关问题