One to one relationship, different key column name, Entity Framework, Code First approach
I have two tables that have already been created. Document
and DocumentStyle
. They have a one to one relationship through the DocumentID
column. However, it is called Id
in Document
table, and DocumentId
in DocumentStyle
table.
Something like this
Document DocumentStyle
|----------| |----------------|
|Id - Key |<------>|DocumentId- key |
|Name-VChar| |Color -VChar|
|Desc-VChar| |Font VChar |
|----------| |----------------|
I'm getting the following error in VS
The ForeignKeyAttribute on property 'DocumentStyle' on type 'KII.Models.Document' is not valid. The foreign key name 'DocumentId' was not found on the dependent type 'KII.Models.Document'. The Name value should be a comma separated list of foreign key property names.
This is part of the code for the Document model class
[ForeignKey("DocumentId")]
public DocumentStyle DocumentStyle { get;set; }
EDIT:
This is the code of my classes.
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; }
[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 { g开发者_StackOverflow社区et; set; }
[DisplayName("Logo")]
public byte[] Logo { get; set; }
}
ForeignKey
attribute pairs foreign key property and navigation property. It doesn't define property from related table! So you must use either:
public class Document
{
public int Id { get; set; }
[ForeignKey("Id")]
public DocumentStyle DocumentStyle { get; set; }
}
if Document
is dependent entity or:
public class DocumentStyle
{
public int DocumentId { get; set; }
[ForeignKey("DocumentId")] // Should not be needed
public Document Document { get; set; }
}
if DocumentStyle
is dependent
精彩评论