When I perform a query such as:
_domainContext.Users
.Where(u => u.Id == _userContext.User.Id)
.SelectMany(u => u.Houses)
... // more query stuff here
I have no problems.
But when I do:
_domainContext.Users
.Where(u => u.Id == _userContext.User.Id)
.First()
.Houses
... // more query stuff here
errors occur such as
A relationship multiplicity constraint violation occurred: An
EntityReference can have no more than one related object, but the
query returned more than one related object.
related to things in the // more query stuff here I think. Can someone explain what the difference between those two is and why one would cause problems?
I am not 100% sure, but I have an inkling as to what the problem is. It is most likely more related to your entity mappings (I am assuming this is an EF project). If you try to actually access your .Houses property in your first query, you will probably get the same error. Just the first one does not actually load the Houses (this can be verified by running a SQL trace and seeing the SQL that is invoked in each query).
I am guessing that somewhere you have code that is something like this?
If that is correct, you need to change your .WithOptional() to .WithMany(). This mapping is not prepared for a 1-to-many relationship that seems to be happening here. If this is not correct, could you please post your model mappings at least, and potentially the generated SQL would be nice also.