I have seen all over that most people are getting this error when using SingleOrDefault. I, however, am using FirstOrDefault. Has anyone seen this anomoly before? I am using the Repository Pattern in order to use Dependency Injection.

FirstOrDefault will not throw that message. Please provide a stack trace and ensure the line number matches the above. (also make sure you don't have any compiler errors such that you might accidentally be running an older copy of the code that might have used SingleOrDefault)
–
Kirk WollJan 26 '12 at 19:59

2

Is it possible that context.Users or p.Username is calling a collection under the hood that, deep down, has a SingleOrDefault() getting called?
–
eouw0o83hfJan 26 '12 at 20:00

2

@BNL You do see that it says FirstOrDefault at the bottom of the stacktrace?
–
Oskar KjellinJan 26 '12 at 20:09

3

Look at the top of the stack trace :) There's some kind of deferred execution that's calling SingleOrDefault.
–
jrummellJan 26 '12 at 20:10

This error is not a matter of query but a matter of mapping configuration. It seems that regardless the query you are calling the EF code first will throw the error when a duplicate matching property is found on the class. I am guessing that it is looping through PropertyInfos with in the class public setters. There can be duplicate property names either because c# is case sensitive so 'MyProperty', 'myproperty' both map to the same column, or in my case because of custom class indexers.

So for class indexers:
The .net framework reflects indexers as properties with parameters. There is currently no way to ignore a custom indexer in an EntityFramework code first mapping file because the lambda expression has no way of expressing these kind of special properties. As this was not enough, my class had an overloaded indexer (hence the error 'Sequence contains more than one matching element')

Sorry didn't mean calling twice, I mean the same expression is there twice. It is separated by the coalescing operator, but I could not find any different between both sides of the '??' operator. The delegate appears to be the same.
–
Phil BolducJan 26 '12 at 23:20

@PhilBolduc, true, I could not either. Good catch.
–
bdparrishJan 27 '12 at 0:49