Thursday, August 27, 2015

EF natural keys and duplicated properties

While working with a VS solution that has been around for about 6 years, an issue was brought to my attention regarding duplicate records from a query. The solution uses and old version of Entity Framework and ties the results of an imported stored procedure to a web forms control (data grid, repeater, or similar). The entity type is based on and mapped to the returned data table from the stored procedure.


A more concrete example would be a Teacher entity with three properties [Name, Classroom, School].


The results of the stored procedure represented below:


June Ward, 102, James Woods High School;
Fiedora Vilma, 233, MLK High School;
Fiedora Vilma, 126, MLK High School;


Were showing up in the app as:


June Ward, 102, James Woods High School;
Fiedora Vilma, 233, MLK High School;
Fiedora Vilma, 233, MLK High School;


Notice that 233 is repeated for the last 2 rows.


Upon investigation of the Teacher entity via the model designer, I saw that only the FullName property was designated as the entity key. But it seems as if the natural key in this set would need to be the FullName and ClassroomNumber.


> Highlight FullName, properties, set entity key false.
> Highlight FullName AND Classroom, properties, entity key true.


now the combo key is shown. Rebuild solution, check to see and the classrooms are no longer repeated.


Wonder if this was "fixed" in some later version of either EF or ASP.NET. Anyways, the solution is fixed for now.

No comments:

Post a Comment