Sam Holder
Sam Holder

Reputation: 32936

Why does my entity framework model only return a single child even though there are multiple children in the relationships?

Using EntityFramework V6.

I have a model which I believe looks right when I view it in the model browser.

My relationship is defined as a 1 to many relationship and in the data I have many children with the defined values in the ReferentialConstraint.

However when I load an instance of the parent object in code and then query the property containing the child elements, the property only ever has a single element in it, which seems to be a random child element.

What might cause this?

the xml of the association looks like this:

    <Association Name="ABAssociation">
      <End Type="Model.A" Role="A" Multiplicity="1" />
      <End Type="Model.B" Role="B" Multiplicity="*" />
      <ReferentialConstraint>
        <Principal Role="A">
          <PropertyRef Name="Field1" />
          <PropertyRef Name="Field2" />
          <PropertyRef Name="Field3" />
          <PropertyRef Name="Field4" />
        </Principal>
        <Dependent Role="B">
          <PropertyRef Name="Field1" />
          <PropertyRef Name="Field2" />
          <PropertyRef Name="Field3" />
          <PropertyRef Name="Field4" />
        </Dependent>
      </ReferentialConstraint>
    </Association>

The model view shows a navigation property on A which highlights all the right fields on B when I select it. there is no Navigation property on B but that is as it should be I think.

B has more fields that make up its primary key then the 4 used, in case that makes a difference, but A has the 4 fields as the primary key.

and the code to load it looks like this:

IRepository<A> aRepository = new ARepository<Entities>();
A a = ARepository.GetAll().FirstOrDefault(x => x.Field1 == 1 
                                            && x.Field2 == 6
                                            && x.Field3 == 151
                                            && x.Field4 == 4));
Console.WriteLine(a.Bs.Count); <-- always 1

What else can I check? Or what am I missing?

EDIT

after a bit more investigation I have a little more information.

B has 2 additional fields, one of which is part of the primary key of the table. In my data I have differences in Field5 of B and am expecting to get 2 rows with Fields1-4 identical and Field5 different.

If I try and select just the B instances like so:

IEnumerable<B> bs = BRepository.GetAll().Where(x => x.Field1 == 1
                                             && x.Field2 == 6
                                             && x.Field3 == 151
                                             && x.Field4 == 4).ToList();

Then I get 2 instances as I expect, but the values in the objects Field5 are the same.

Why could this be happening?

Upvotes: 1

Views: 89

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109109

As is apparent from your comment, applying the predicate x => x.Field1 == 1 ... to the Bs directly resulted in identical B objects to be materialized. This usually indicates that the primary key in the EF context is less discriminative than the PK in the database.

I didn't know that in a collection navigation property this causes the collection to have less (but apparently unique) items than expected, so to me this is a new diagnostic for primary key problems.

Upvotes: 1

Related Questions