【问题标题】:One to one relationship, different key column name, Entity Framework, Code First approach一对一关系、不同的键列名称、实体框架、代码优先方法
【发布时间】:2011-08-25 01:49:50
【问题描述】:

我已经创建了两个表。 DocumentDocumentStyle。它们通过DocumentID 列具有一对一的关系。但是,在Document 表中称为Id,在DocumentStyle 表中称为DocumentId

类似的东西

>  Document            DocumentStyle 
> |----------|        |----------------|
> |Id - Key  |<------>|DocumentId- key |
> |Name-VChar|        |Color     -VChar|
> |Desc-VChar|        |Font      VChar |
> |----------|        |----------------|

我在 VS 中遇到以下错误

属性上的 ForeignKeyAttribute 'DocumentStyle' 类型 “KII.Models.Document”无效。 外键名称“DocumentId”是 在依赖类型上找不到 'KII.Models.Document'。名称值 应该是一个逗号分隔的列表 外键属性名称。

这是文档模型类的部分代码

[ForeignKey("DocumentId")]  public
DocumentStyle DocumentStyle { get;set; }

编辑:

这是我的课程的代码。

public class Document
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public int FundId { get; set; }
        public int ClientId { get; set; }

        [ForeignKey("FundId")]
        public Fund Fund { get; set; }

        [ForeignKey("ClientId")]
        public Client Client { get; set; }
        //public ImageWrapper Logo { get; set; }

        [ForeignKey("ID")]
        public DocumentStyle DocumentStyle { get; set; }

        public Document()
        {

        }

        public Document(DocumentStyle documentStyle)
        {
            DocumentStyle = documentStyle;
        }

    }


public class DocumentStyle
    {

        public DocumentStyle()
        {

        }

        [Key]
        [DisplayName("Document ID")]
        public int DocumentId { get; set; }

        [ForeignKey("DocumentId")]
        public Document Document { get; set; }

        [DisplayName("Title Foreground Color")]
        public string TitleForegroundColor { get; set; }

        [DisplayName("Title Background Color")]
        public string TitleBackgroundColor { get; set; }

        [DisplayName("Title Font Family")]
        public string TitleFontFamily { get; set; }

        [DisplayName("Title Font Size")]
        public string TitleFontSize { get; set; }

        [DisplayName("Title Font Style")]
        public string TitleFontStyle { get; set; }

        [DisplayName("Title Font Weight")]
        public string TitleFontWeight { get; set; }

        [DisplayName("Title Text Decoration")]
        public string TitleTextDecoration { get; set; }

        [DisplayName("Section Title Foreground Color")]
        public string SectionTitleForegroundColor { get; set; }

        [DisplayName("Section Title Background Color")]
        public string SectionTitleBackgroundColor { get; set; }

        [DisplayName("Section Title Font Family")]
        public string SectionTitleFontFamily { get; set; }

        [DisplayName("Section Title Font Size")]
        public string SectionTitleFontSize { get; set; }

        [DisplayName("Section Title Font Styled")]
        public string SectionTitleFontStyle { get; set; }

        [DisplayName("Section Title Font Weight")]
        public string SectionTitleFontWeight { get; set; }

        [DisplayName("Section Title Text Decoration")]
        public string SectionTitleTextDecoration { get; set; }

        [DisplayName("Paragraph Foreground Color")]
        public string ParagraphForegroundColor { get; set; }

        [DisplayName("Paragraph Background Color")]
        public string ParagraphBackgroundColor { get; set; }

        [DisplayName("Paragraph Font Family")]
        public string ParagraphFontFamily { get; set; }

        [DisplayName("Paragraph Font Size")]
        public string ParagraphFontSize { get; set; }

        [DisplayName("Paragraph Font Style")]
        public string ParagraphFontStyle { get; set; }

        [DisplayName("Paragraph Font Weight")]
        public string ParagraphFontWeight { get; set; }

        [DisplayName("Paragraph Text Decoration")]
        public string ParagraphTextDecoration { get; set; }

        [DisplayName("Logo")]
        public byte[] Logo { get; set; }

    }

【问题讨论】:

    标签: c# ef-code-first entity-framework-4.1


    【解决方案1】:

    ForeignKey 属性对外键属性和导航属性。它没有从相关表中定义属性!所以你必须使用:

    public class Document
    {
        public int Id { get; set; }
        [ForeignKey("Id")]
        public DocumentStyle DocumentStyle { get; set; }
    }
    

    如果Document 是依赖实体或:

    public class DocumentStyle
    {
        public int DocumentId { get; set; }
        [ForeignKey("DocumentId")] // Should not be needed
        public Document Document { get; set; }
    }
    

    如果DocumentStyle 是依赖的

    【讨论】:

    • 太棒了。我弄错了。它成功了。我通过了那个错误。但是,我现在得到了这个。 " 关系'Document_DocumentStyle'中的角色'Document_DocumentStyle_Source'中的多重性无效。因为从属角色是指关键属性,所以从属角色的多重性上限必须为1"
    • 显示您的实体或流畅的映射。看起来您正在尝试在不可能的情况下定义一侧可选。
    • ForeignKey 属性只能用于定义外键的关系的一侧。我猜Document 是您模型中的主体,因此请从DocumentStyle 属性中删除该属性。
    • 太好了,你就是那个男人!那成功了。但是,我在 Document 实例中对 DocumentStyle 的引用为空。
    • 您必须显式加载关系或将导航属性设为虚拟。
    猜你喜欢
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多