c# - 名称不匹配时的实体框架外键
问题描述
我有一个表AccountValues
,我想从我的Account
表中引用它。但是,与其使用AccountValuesId
and AccountValues
,我希望它通过以下前缀更具描述性MostRecent_
:
[ForeignKey("AccountValues")]
public long MostRecent_AccountValuesId { get; set; }
public AccountValues MostRecent_AccountValues { get; set; }
我的问题是我应该在哪里放置我的ForeignKey
属性,以便我最终得到一个实际的外键(例如FK_Something
)并且还可以MostRecent_AccountValues
自动工作?
注意:我的约定是不要有复数表名。但是这里AccountValues
是复数只是因为每一行都有许多不同的值。
解决方案
问题出在你的ForeignKey
名字上public long MostRecent_AccountValuesId
。ForeignKey
名称应与导航属性名称匹配,如下所示:
[ForeignKey("MostRecent_AccountValues")] // <-- Here it is
public long MostRecent_AccountValuesId { get; set; }
public AccountValues MostRecent_AccountValues { get; set; }
推荐阅读
- macos - AppleScript:编辑完整编写的脚本购买“删除”某些命令,但保留它以便您记住它
- excel - 来自 Excel 的电子邮件导致 excel 挂起
- sql-server - Azure Synapse Analytics 中的递归查询
- windows - ffmpeg:如何将 SSIM 和 PSNR 保存到文件中?
- javascript - 不使用 App.vue 时带有 Vite 的 Vue 3 渲染空白页面
- sql - 为什么连接查询给出与子查询不同的结果?
- java - 反射检查包含类中是否存在字段
- vue.js - 从 Nuxt 上传到 AWS S3 存储桶时出现 500 内部服务器错误
- perl - 如何用 Perl 提取某些行?
- amazon-web-services - 如何将 .pem 文件导入 AWS Lambda 函数以及如何将其作为 SFTP 连接的路径