I have one table "Courses" mapped to two entities "Course" and "CourseDetails".
Both have POCO classes with virtual navigation properties. Both entities use same ID (Identity on Course but not on CourseDetail). Mapping between entities is a "referential integrity constraint" not in "mappings" window. Course is principle in constraint.I am using LazingLoading and ProxyCreationEnabled.
After calling context.SaveChanges() the connects look fine (IDs, references and data).
When access开发者_StackOverflow社区ing course.CourseDetails from within a loop on context.Courses I get the default empty CourseDetails created by Course's constructor instead of the actual CourseDetails data!?
When I call context.LoadProperty(course, c => c.CourseDetails) I get an InvalidOperationException with the message:
"Multiplicity constraint violated. The role 'CourseDetails' of the relationship 'OrmComparisons.EFPocos1.CourseDetailsCourse' has multiplicity 1 or 0..1."
Question 1: Why is course.CourseDetails empty?
Question 2: Why am I receiving an exception? Question 3: What should I be doing instead?I can share the solution + sql file if anyone wants to look.
After comparing self written POCOs with template generated POCOs,
I noticed the Fix___ methods, which are meant to fix navigation properties.
Some how all other types of relationships (one to many, many to many, inheritance)
work with virtual properties and lazy loading.
Only the table splitting to one-to-one relationship.
Anyway - fix was using t4 generated POCOs and not self written POCOs with simple { get; set; } properties.
精彩评论