无法确定外键的复合外键顺序

编程入门 行业动态 更新时间:2024-10-26 09:34:37
本文介绍了无法确定外键的复合外键顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

人员是包括所有用户的用户模型.更改模型包括EngineerId和ManagerId,它们都是Person ID.为什么会出现此错误?

Person is the user model that includes all users. The change model includes the EngineerId and the ManagerId, both are Person IDs. Why am I getting this error?

无法确定类型为ProjectName.Models.Change的外键的复合外键顺序.在复合外键属性上使用ForeignKey数据批注时,请确保使用Column数据批注或流利的API指定顺序.

Unable to determine a composite foreign key ordering for foreign key on type ProjectName.Models.Change. When using the ForeignKey data annotation on composite foreign key properties ensure order is specified by using the Column data annotation or the fluent API.

public class Change { [Key] public int ChangeId { get; set; } [Required(ErrorMessage = "Change description is required.")] [Display(Name = "Change Description")] [DataType(DataType.MultilineText)] public string ChangeDescription { get; set; } [Required(ErrorMessage = "Change date is required.")] [Display(Name = "Date of Change")] [DataType(DataType.Date)] public DateTime ChangeDate { get; set; } [Required(ErrorMessage = "Time is required.")] [Display(Name = "Time of Change")] [DataType(DataType.Time)] public DateTime ChangeTime { get; set; } [Required(ErrorMessage = "Engineer name is required.")] [Display(Name = "Engineer")] [ForeignKey("person")] public int EngineerId { get; set; } [Required(ErrorMessage = "Status is required.")] [Display(Name = "Status")] public int StatusId { get; set; } [Required(ErrorMessage = "Manager is required.")] [Display(Name = "Manager")] [ForeignKey("person")] public int ManagerId { get; set; } [Required(ErrorMessage = "System is required.")] [Display(Name = "System")] public int SystemDetailId { get; set; } public virtual Person person { get; set; } public virtual Status status { get; set; } public virtual SystemDetail systemdetail { get; set; } }

推荐答案

您具有两个关联的外键属性( EngineerId 和 ManagerId )(通过 ForeignKey 属性),并具有相同的导航属性( person ).EF通过这种方式认为它们构成了一个复合密钥,因此您将获得异常.

You have 2 foreign key properties (EngineerId and ManagerId) that you have associated (via ForeignKey attribute) with the one and the same navigation property (person). This way EF thinks they form a composite key, hence the exception you are getting.

由于(如果我理解正确),您的意图是建立2个关系,因此需要将每个外键与单独的导航属性相关联.这是您需要更改的必要部分:

Since (if I understand correctly) your intent is to have 2 relationships, you need to associate each foreign key with a separate navigation property. Here is the essential part that you need to change:

public class Change { // ... [Required(ErrorMessage = "Engineer name is required.")] [Display(Name = "Engineer")] [ForeignKey("Engineer")] public int EngineerId { get; set; } [Required(ErrorMessage = "Manager is required.")] [Display(Name = "Manager")] [ForeignKey("Manager")] public int ManagerId { get; set; } // instead of person property: public virtual Person Engineer { get; set; } public virtual Person Manager { get; set; } }

更多推荐

无法确定外键的复合外键顺序

本文发布于:2023-10-29 02:56:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!