一对一的关系,不同的关键列名,实体框架,代码第一的方法 [英] One to one relationship, different key column name, Entity Framework, Code First approach
问题描述
文件
和 DocumentStyle
。他们通过 DocumentID
列具有一对一的关系。但是,在文档
表和 DocumentId
Id > DocumentStyle
表。 这样的一个
>文件DocumentStyle
> | ---------- | | ---------------- |
> | Id - Key |< ------> | DocumentId- key |
> |名称-VCHAR | |颜色-VChar |
> |降序排序VCHAR | |字体VChar |
> | ---------- | | ---------------- |
我在VS中收到以下错误
类型为
'KII.Models.Document'的属性
'DocumentStyle'上的ForeignKeyAttribute无效。
外部键名称DocumentId是
在依赖类型
'KII.Models.Document'上找不到。名称值
应为逗号分隔的
外键属性名称列表。
这是Document模型类的代码
[ForeignKey(DocumentId)] public
DocumentStyle DocumentStyle {get; set ;
编辑:
这是我的课程代码
public class Document
{
[Key ]
public int ID {get;组; }
public string Name {get;组; }
public int FundId {get;组; }
public int ClientId {get;组; }
[ForeignKey(FundId)]
公共基金基金{get;组; }
[ForeignKey(ClientId)]
public Client Client {get;组; }
// public ImageWrapper Logo {get;组; }
[ForeignKey(ID)]
public DocumentStyle DocumentStyle {get;组;
public Document()
{
}
public Document(DocumentStyle documentStyle)
{
DocumentStyle = documentStyle;
}
}
public class DocumentStyle
{
public DocumentStyle()
{
}
[Key]
[DisplayName(Document ID)]
public int DocumentId {get;组; }
[ForeignKey(DocumentId)]
public Document Document {get;组; }
[DisplayName(Title Foreground Color)]
public string TitleForegroundColor {get;组; }
[DisplayName(Title Background Color)]
public string TitleBackgroundColor {get;组; }
[DisplayName(Title Font Family)]
public string TitleFontFamily {get;组; }
[DisplayName(Title Font Size)]
public string TitleFontSize {get;组; }
[DisplayName(Title Font Style)]
public string TitleFontStyle {get;组; }
[DisplayName(Title Font Weight)]
public string TitleFontWeight {get;组; }
[DisplayName(Title Text Decoration)]
public string TitleTextDecoration {get;组; }
[DisplayName(Section Title Foreground Color)]
public string SectionTitleForegroundColor {get;组; }
[DisplayName(Section Title Background Color)]
public string SectionTitleBackgroundColor {get;组; }
[DisplayName(Section Title Font Family)]
public string SectionTitleFontFamily {get;组; }
[DisplayName(Section Title Font Size)]
public string SectionTitleFontSize {get;组; }
[DisplayName(Section Title Font Styled)]
public string SectionTitleFontStyle {get;组; }
[DisplayName(Section Title Font Weight)]
public string SectionTitleFontWeight {get;组; }
[DisplayName(Section Title Text Decoration)]
public string SectionTitleTextDecoration {get;组; }
[DisplayName(Paragraph Foreground Color)]
public string ParagraphForegroundColor {get;组; }
[DisplayName(Paragraph Background Color)]
public string ParagraphBackgroundColor {get;组; }
[DisplayName(Paragraph Font Family)]
public string ParagraphFontFamily {get;组; }
[DisplayName(Paragraph Font Size)]
public string ParagraphFontSize {get;组; }
[DisplayName(Paragraph Font Style)]
public string ParagraphFontStyle {get;组; }
[DisplayName(Paragraph Font Weight)]
public string ParagraphFontWeight {get;组; }
[DisplayName(Paragraph Text Decoration)]
public string ParagraphTextDecoration {get;组; }
[DisplayName(Logo)]
public byte [] Logo {get;组;
}
ForeignKey
属性对外键属性和导航属性。它没有从相关表中定义属性!所以你必须使用:
public class Document
{
public int Id {get;组; }
[ForeignKey(Id)]
public DocumentStyle DocumentStyle {get;组;
}
如果文档
是依赖实体,或者:
public class DocumentStyle
{
public int DocumentId {get;组; }
[ForeignKey(DocumentId)] //不需要
public Document Document {get;组;
}
if DocumentStyle
是依赖
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; } //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; } }
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
这篇关于一对一的关系,不同的关键列名,实体框架,代码第一的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!