user547794
user547794

Reputation: 14511

EF one to one relationship when neither are primary keys

I have 2 entities that need to map together. Since they are both database views, neither is technically a primary key.

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

If I try to access MemberInfo from ChangeLog I get an error saying:

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.

The MemberID field for MemberInfo is unique. I need to be able to access the MemberInfo properties from ChangeLog through the MemberInfo Property.

Upvotes: 1

Views: 126

Answers (1)

saber safavi
saber safavi

Reputation: 452

this error is for Asset property in ChangeLog class you dont write that class i think with changing that property you can fix this error

Upvotes: 1

Related Questions