gpt4 book ai didi

c# - 一对一关系、不同的键列名称、 Entity Framework 、代码优先方法

转载 作者:太空狗 更新时间:2023-10-29 23:10:39 25 4
gpt4 key购买 nike

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

像这样

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

我在 VS 中遇到以下错误

The ForeignKeyAttribute on property 'DocumentStyle' on type'KII.Models.Document' is not valid.The foreign key name 'DocumentId' wasnot found on the dependent type'KII.Models.Document'. The Name valueshould be a comma separated list offoreign key property names.

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

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

编辑:

这是我的类(class)代码。

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 { get; set; }

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

最佳答案

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 是依赖的

关于c# - 一对一关系、不同的键列名称、 Entity Framework 、代码优先方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5994624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com