REPLAY_5
REPLAY_5

Reputation: 11

Cross including the same table relations in EF Code Throws an error Multiplicity constraint violated

I have cross relations with the same table, in 3rd object. When I try to insert new object, got an error :

Multiplicity constraint violated. The role 'OrgOwners_Organisation_Target' of the relationship 'GBankDataSource.OrgOwners_Organisation' has multiplicity 1 or 0..1.

I've tried to annotate [ForeignKey("...")] in any of the classes, but nothing happend. EF allways choose one field (OrgRefID in this sample) and use it or both relations, while OrgID are not used.

public class OrganisationInfo
{
    [Key]
    public int OrgID { get; set; }
...
    public virtual List<OrgOwners> OrgOwners { get; set; } // object that throws error 

}


public class OrgOwners
{
    [Key]
    public int OrgOwnerID { get; set; }

    public int OrgID { get; set; } //Suppose to be a ForeignKey for (OrganisationInfo OrgOwners List)
    public int? OrgRefID { get; set; }
...
    [ForeignKey("OrgRefID")]
    public virtual OrganisationInfo Organisation { get; set; } //(Suppose to use OrgRefID as ForeignKey)

}

When I add a record to OrgOwners without Organisation ( Organisation =null) - it is OK. But when I do

var first = new OrganisationInfo(); //First organisation DB.OrganisationInfoes.Add(first);

var nextOrg = new OrganisationInfo(); //second organisation

first.OrgOwners = new list();

var Owner = new OrgOwners(); Owner.Organsiation = nextOrg;

first.OrgOwners.Add(Owner); // Add Owner with the second organisation to the First one.

I got an error.

Multiplicity constraint violated.

OrgOwner.Organisation - is NOT the same OrganisationInfo as in root of OrgOwners list. It must be different OrganisationInfo items, related to OrgRefID ForeignKey.

Upvotes: 1

Views: 45

Answers (2)

 REPLAY_5
REPLAY_5

Reputation: 11

Full worked example:

public class OrganisationInfo
{
    [Key]
    public int OrgID { get; set; }
    public virtual List<OrgOwners> OrgOwners { get; set; } 

}

public class OrgOwners
{
    [Key]
    public int OrgOwnerID { get; set; }
    public int OrgID { get; set; }
    public int? OrgRefID { get; set; }

    [ForeignKey("OrgRefID")]
    public virtual OrganisationInfo Organisation { get; set; } 

}



modelBuilder.Entity<OrganisationInfo>()
                    .HasMany(e => e.OrgOwners)
                    .WithRequired() 
                    .HasForeignKey(e => e.OrgID); 

Upvotes: 0

Ivan Stoev
Ivan Stoev

Reputation: 205599

It's because EF by default automatically "pairs" the navigation properties where possible to form a relationship. In your case, it pairs OrganizationInfo.OrgOwners collection navigation property with OrgOwners.Organization reference navigation property, hence takes and uses the associated with it OrgRefID FK.

One way to resolve the issue is to add a second reference navigation property to OrgOwners and associate it with the OrgID property via ForeignKey attribute and OrganizationInfo.OrgOwners collection navigation property via InverseProperty attribute:

public int OrgID { get; set; } //Suppose to be a ForeignKey for (OrganisationInfo OrgOwners List)

[ForeignKey("OrgID")]
[InverseProperty("OrgOwners")]
public virtual OrganisationInfo OwnerOrganization { get; set; }

To do that without changing the entity model, you should configure the relationship via fluent API:

modelBuilder.Entity<OrganisationInfo>()
    .HasMany(e => e.OrgOwners)
    .WithRequired() // no inverse navigation property
    .HasForeignKey(e => e.OrgID); // <--

Upvotes: 1

Related Questions