一对一关系、不同的键列名称、实体框架、代码优先方法

2024-02-07

我有两个已经创建的表。Document and DocumentStyle。他们通过以下方式建立一对一的关系DocumentID柱子。然而,它被称为Id in Document表,以及DocumentId in DocumentStyle table.

像这样的东西

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

我在VS中遇到以下错误

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

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

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

EDIT:

这是我的课程的代码。

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; }
}

if Document是依赖实体或:

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

if DocumentStyle是依赖的

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一对一关系、不同的键列名称、实体框架、代码优先方法 的相关文章

  • #include 在 ubuntu 中“没有这样的文件”

    当使用 g std c 0x Wall test cc o hello 编译时 输出致命错误 cstdatomic 没有这样的文件或直接 哪里不见了 包含内容应该是 include
  • C# AES Rijndael - 检测无效密码

    我正在使用 Rijndael 加密程序中的一些敏感数据 当用户输入错误的密码时 大多数情况下CryptographicException抛出消息 填充无效且无法删除 然而 CryptStream 不会抛出密码错误的异常 而是返回错误解密的流
  • C++17 中带有 noexcept 的 std::function

    在 C 17 中noexcept 已添加到类型系统中 http www open std org jtc1 sc22 wg21 docs papers 2015 p0012r1 html void r1 void f noexcept f
  • 将内核链接到 PTX 函数

    我可以使用 PTX 文件中包含的 PTX 函数作为外部设备函数 将其链接到另一个应调用该函数的 cu 文件吗 这是另一个问题CUDA 将内核链接在一起 https stackoverflow com questions 20636800 c
  • NUnit 测试运行顺序

    默认情况下 nunit 测试按字母顺序运行 有谁知道有什么方法可以设置执行顺序吗 是否存在这样的属性 我只是想指出 虽然大多数受访者认为这些是单元测试 但问题并没有具体说明它们是 nUnit 是一个很棒的工具 可用于各种测试情况 我可以看到
  • ObservableCollection 上的“Cascade”绑定,包含其他 ObservableCollection

    我有一个项目需要显示合同列表 Class Affaire 每个合约都有一个阶段列表 类别阶段 我使用绑定在 2 个不同的 ListView 中显示它们中的每一个 问题是当我从 ListView 中删除一个 Phase 时 显示 Phases
  • 如何获取任意类型的默认值

    在 C 中我可以写这样的东西 class AnyThing
  • 使用 boost::iterator_facade<>

    我有一个链表结构 struct SomeLinkedList const char bar int lots of interesting stuff in here DWORD foo SomeLinkedList pNext 它是现有
  • CMake - 未定义参考

    我正在尝试将 gtest 包含到我的项目中 问题是我在 GTest 中收到未定义的引用错误 我正在尝试在 Gtest 中测试 Node 类 在节点的构造函数中 我使用类记录器 尽管我已将库记录器添加到 gtest target 中 但我仍然
  • .NET 中 IEqualityComparer 中 GetHashCode 的作用是什么?

    我试图了解 IEqualityComparer 接口的 GetHashCode 方法的作用 下面的例子取自MSDN using System using System Collections Generic class Example st
  • 如何忽略搜索条件中的空属性

    我有一个不好的要求要做 无论如何 我必须在我的应用程序中实现它 我有一个Track class public class Track public string Name get set public string City get set
  • 复杂的 C 声明

    我刚刚在互联网上浏览了一些代码 发现了这个 float foo SIZE SIZE 我如何阅读这份声明 是否有一套特定的规则来阅读如此复杂的声明 我有一段时间没做这个了 从 开始foo然后向右走 float foo SIZE SIZE fo
  • C# Linq 可以做组合数学吗?

    我有这个数据结构 class Product public string Name get set public int Count get set var list new List
  • DLL 中的 XP 风格组合框

    我需要使用 C 和 WIN32 API 无 MFC 在 DLL 中创建 XP 风格的组合框 我设法在 DLL 中创建控件 不是以 XP 风格 我设法在带有清单的 exe 中创建 XP 样式组合框 但它在 DLL 中不起作用 为了让您的 DL
  • C++ std:.auto_ptr 或 std::unique_ptr (支持多个编译器,甚至是旧的 C++03 编译器)?

    我正在尝试更新一些 C 代码 我想转向更现代的代码 c 11 但我仍然需要使用一些较旧的编译器 兼容 c 03 来编译代码 因为支持的平台限制 我知道在 C 11 编译器中 std auto ptr 已被弃用 但由于较旧的编译器支持 我不能
  • 派生类的聚合初始化

    以下代码无法使用 Visual Studio2017 或在线 GDB 进行编译 我期望它能够编译 因为迭代器只是一个具有类型的类 并且它是从公共继承的 这是不允许的还是在 VS2017 中不起作用 template
  • 将函数作为函数参数传递

    Unity C 似乎无法识别Func lt gt 作为函数委托的符号 那么 如何将函数作为函数参数传递呢 我有一个想法Invoke functionName 0 可能有帮助 但我不确定它是否实际上立即调用该函数 或者等待帧结束 还有别的办法
  • C# 记录类型:记录子类之间的相等比较

    给定父记录类型 public record Foo string Value 和两个记录子类Bar and Bee我想知道是否可以实施Equals在基类中 因此 Foo Bar 或 Bee 的实例都被考虑equal基于Value 两者都与E
  • 即使对于新上下文,OnModelCreating 也仅调用一次

    我有多个相同但内容不同的 SQL Server 表 在编写代码优先 EF6 程序时 我尝试为每个程序重用相同的数据库上下文 并将表名称传递给上下文构造函数 然而 虽然每次都会调用构造函数 但尽管每次都是从 new 创建数据库上下文 但 On
  • 散列 hash_hmac 时,Convert.ToChar(0) 散列结果与 PHP 中的 chr(0) 不同的字符串

    我在 PHP 中有一个字符串 它被转换为字节数组并进行哈希处理 转换为字节数组的字符串如下所示 G 字符 0 便便 我需要 C 中的等效字节数组 这样我才能得到相同的哈希值 编辑 这是完整的问题 生成的哈希值不同 PHP api secre

随机推荐