5
我有兩個表已經被創建。 Document
和DocumentStyle
。他們通過DocumentID
列有一對一的關係。然而,Id
表在Document
表中,DocumentId
表在DocumentStyle
表中。一對一關係,不同的關鍵列名稱,實體框架,代碼優先的方法
像這樣的事情
> 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; } }
大。我錯了。它做了詭計。我通過了這個錯誤。不過,我現在得到這個。 「多重性在'Document_DocumentStyle'關係中的'Document_DocumentStyle_Source'中無效,因爲從屬角色是指關鍵屬性,所以從屬角色的多重性的上限必須爲1」 – Omar 2011-05-13 16:57:23
顯示您的實體或流利的映射。這看起來像你試圖在不可能的情況下定義一個可選的方面。 – 2011-05-13 17:03:47
ForeignKey屬性必須僅用於定義外鍵的關係的一側。我猜'Document'在你的模型中是主體,所以從'DocumentStyle'屬性中刪除屬性。 – 2011-05-13 17:19:31