当两者都不是主键时的一对一关系
本文关键字:一对一 关系 两者都 | 更新日期: 2023-09-27 17:49:56
我有两个实体需要映射在一起。因为它们都是数据库视图,所以从技术上讲它们都不是主键。
[Table("Name")]
public class MemberInfo
{
[Key]
[Column("id")]
public string MemberId { get; set; }
[Column("first_name")]
public string FirstName { get; set; }
[Column("last_name")]
public string LastName { get; set; }
public string Designation { get; set; }
[Column("full_name")]
public string FullName { get; set; }
}
public class ChangeLog
{
[Column("ID")]
public int ID { get; set; }
[Column("asset_id")]
public virtual Asset AssetID { get; set; }
[Column("member_id")]
public int MemberID { get; set; }
public virtual MemberInfo MemberInfo { get; set; }
[Column("comment")]
public string Comment { get; set; }
[Column("createdDT")]
public DateTime CreatedDT { get; set; }
}
如果我尝试从ChangeLog访问MemberInfo,我得到一个错误说:
A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.
MemberInfo的MemberID字段是唯一的。我需要能够通过MemberInfo属性从ChangeLog访问MemberInfo属性。
此错误是针对ChangeLog类中的Asset属性你不用写那个类我认为通过改变属性可以修复这个错误