Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:03:04+00:00 2026-05-28T16:03:04+00:00

I have seen all over that most people are getting this error when using

  • 0

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.

return context.Users.FirstOrDefault(p => p.Username.ToLower() == username.ToLower());

EDIT

See below: The error comes from internal code to the EntityFramework from what I can tell.

[InvalidOperationException: Sequence contains more than one matching element]
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source, Func`2 predicate) +2668318
   System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConventionImpl.MatchKeyProperty(EdmEntityType entityType, IEnumerable`1 primitiveProperties) +121
   System.Data.Entity.ModelConfiguration.Conventions.KeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model) +72
   System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model) +17
   System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.Dispatch(TEdmDataModelItem item) +100
   System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item) +22
   System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection(IEnumerable`1 collection, Action`1 visitMethod) +138
   System.Data.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes) +75
   System.Data.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item) +88
   System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item) +31
   System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection(IEnumerable`1 collection, Action`1 visitMethod) +138
   System.Data.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces) +75
   System.Data.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item) +56
   System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmModel(EdmModel item) +44
   System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model) +126
   System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +125
   System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +165
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +61
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +111
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +417
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +63
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
   System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate) +63
   Entities.User.GetCurrentPerson(String username, KmManagerDbContext context) in C:\Users\user\Documents\Visual Studio 2010\Projects\KmManager\Entities\User.cs:85

User.cs

public class User
{
    public long Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Username { get; set; }

    // Custom Propreties
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }

    public string LastNameFirst
    {
        get
        {
            return LastName + ", " + FirstName;
        }
    }

    public static string TableName
    {
        get
        {
            return "Users";
        }
    }

    public static User GetCurrentPerson(string username, KmManagerDbContext context)
    {
        try
        {
            // find the person who has the ad name = username
            return context.Users.FirstOrDefault(p => p.Username.ToLower() == username.ToLower());
        }
        catch (Exception ex)
        {
            throw new ApplicationException("There was an error retrieving the user from the database.", ex);
        }
    }
}

UserConfiguration.cs

public UserConfiguration()
{
    this.ToTable(User.TableName);

    this.HasKey(x => x.Id);

    this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    this.Property(x => x.FirstName).IsRequired();
    this.Property(x => x.LastName).IsRequired();
    this.Property(x => x.Username).IsRequired();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:03:05+00:00Added an answer on May 28, 2026 at 4:03 pm

    If anyone is worried about the answer…

    I split some commonalities amongst my POCOs using BaseEntity.cs

    BaseEntity.cs

    public class BaseEntity<T> where T : BaseEntity<T>
    {
        public long Id { get; set; }
    
        public class Comparer : IEqualityComparer<T>
        {
            public bool Equals(T x, T y)
            {
                if (x.Id == y.Id)
                {
                    return true;
                }
    
                return false;
            }
    
            public int GetHashCode(T obj)
            {
                return (int)obj.Id;
            }
        }
    }
    

    This caused the configuration to have wierd behavior. I changed all the POCOs to the previous state, and everything works as expected. Sorry for the waste of time.

    And User POCO looked like this…

    User.cs

    public class User : BaseEntity<User>
    {
        public string FirstName { get; set; } 
    
        public string LastName { get; set; } 
    
        public string Username { get; set; } 
    
        // Custom Propreties 
        public string FullName 
        { 
            get 
            { 
                return FirstName + " " + LastName; 
            } 
        } 
    
        public string LastNameFirst 
        { 
            get 
            { 
                return LastName + ", " + FirstName; 
            } 
        } 
    
        public static string TableName 
        { 
            get 
            { 
                return "Users"; 
            } 
        } 
    
        public static User GetCurrentPerson(string username, KmManagerDbContext context) 
        { 
            try 
            { 
                // find the person who has the ad name = username 
                return context.Users.FirstOrDefault(p => p.Username.ToLower() == username.ToLower()); 
            } 
            catch (Exception ex) 
            { 
                throw new ApplicationException("There was an error retrieving the user from the database.", ex); 
            } 
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen posts all over the internet that talk about how to fix
I have seen this question asked all over the internet and answered in many
I've looked all over the place, but it seems that examples I have seen
Anyone using WPF for real LOB applications? We have all seen the clever demos
I have seen on many sites the silverlight video player. I noticed that all
I'm sure you've all seen them. Line of Business UIs that have logic such
Most references I've seen, and my IDE's code completion all have my specifying a
I have been getting an error message that I can't resolve. It originates from
All the refactoring tools for C# and VB.Net that I have seen only consider
We have all seen countless instances of forms with a select drop down having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.