Hi Jeroen,
The fundamental reason that you are seeing this behavior is that EF doesn’t support different types in a hierarchy having different primary key definitions. You can see this if you look at the tables that are created—the primary key is RelatieID for both tables. The reason you don’t see any errors from EF is that Code First currently ignores calls to HasKey made on derived types in the hierarchy. I have filed a bug to consider changing this: https://entityframework.codeplex.com/workitem/1465.
Given that the primary key only consists of the RelatieID property/column, it means that all entities with the same RelatieID have the same identity, regardless of what value is in VersieID. This is why there are duplicate results returned from the query. When the first “Customer-With-4-Versions” row is returned from the database an entity instance is created for it in the normal way. When the next row is returned it has the same identity as the previous row. This means that EF will not create a new object but will instead return the object that already exists with that identity. The result is that the same object is returned four times.
Thanks,
Arthur
The fundamental reason that you are seeing this behavior is that EF doesn’t support different types in a hierarchy having different primary key definitions. You can see this if you look at the tables that are created—the primary key is RelatieID for both tables. The reason you don’t see any errors from EF is that Code First currently ignores calls to HasKey made on derived types in the hierarchy. I have filed a bug to consider changing this: https://entityframework.codeplex.com/workitem/1465.
Given that the primary key only consists of the RelatieID property/column, it means that all entities with the same RelatieID have the same identity, regardless of what value is in VersieID. This is why there are duplicate results returned from the query. When the first “Customer-With-4-Versions” row is returned from the database an entity instance is created for it in the normal way. When the next row is returned it has the same identity as the previous row. This means that EF will not create a new object but will instead return the object that already exists with that identity. The result is that the same object is returned four times.
Thanks,
Arthur