当两者都不是主键时,EF一对一关系

时间:2012-12-28 19:44:38

标签: c# asp.net-mvc-3 entity-framework entity-framework-4 linq-to-entities

我有2个需要一起映射的实体。由于它们都是数据库视图,因此技术上也不是主键。

 [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属性。

1 个答案:

答案 0 :(得分:1)

此错误适用于ChangeLog类中的Asset属性 你不写这门课 我认为通过更改该属性,您可以修复此错误