【问题标题】:FirstOrDefault throws 'Sequence contains more than one matching element'FirstOrDefault 抛出“序列包含多个匹配元素”
【发布时间】:2012-07-18 19:49:47
【问题描述】:

我已经看到大多数人在使用SingleOrDefault 时都会遇到此错误。但是,我正在使用FirstOrDefault。有没有人见过这种异常现象?我正在使用存储库模式来使用依赖注入。

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

编辑

见下文:据我所知,错误来自 EntityFramework 的内部代码。

[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

用户.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();
}

【问题讨论】:

  • FirstOrDefault 不会抛出该消息。请提供堆栈跟踪并确保行号与上述匹配。 (还要确保没有任何编译器错误,以免意外运行可能使用过 SingleOrDefault 的旧代码副本)
  • context.Users 或 p.Username 是否有可能在底层调用了一个集合,该集合有一个 SingleOrDefault() 被调用?
  • @BNL 您确实看到堆栈跟踪底部显示 FirstOrDefault?
  • 查看堆栈跟踪的顶部 :) 有某种延迟执行正在调用 SingleOrDefault
  • @bdparrish 您的实体数据模型在执行看似密钥检查的操作时抛出错误,表明支持您的用户实体的任何数据都包含重复的密钥。对于初学者,请在您的数据模型中发布用户实体的屏幕截图。

标签: c# entity-framework-4.1 ef-code-first


【解决方案1】:

如果有人担心答案...

我使用 BaseEntity.cs 在我的 POCO 中拆分了一些共性

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;
        }
    }
}

这导致配置有奇怪的行为。我将所有 POCO 更改为以前的状态,一切都按预期工作。很抱歉浪费了时间。

而用户 POCO 看起来像这样......

用户.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); 
        } 
    } 
} 

【讨论】:

    【解决方案2】:

    这个错误不是查询问题,而是映射配置问题。似乎无论您首先调用 EF 代码的查询都会在类上找到重复的匹配属性时引发错误。我猜它在类公共设置器中循环通过 PropertyInfos。可能有重复的属性名称要么因为 c# 区分大小写,所以 'MyProperty'、'myproperty' 都映射到同一列,或者在我的情况下是因为 自定义类索引器 .

    所以对于类索引器: .net 框架将索引器反映为properties with parameters。目前无法忽略 EntityFramework 代码优先映射文件中的自定义索引器,因为 lambda 表达式无法表达这些特殊属性。由于这还不够,我的班级有一个重载的索引器(因此错误'序列包含多个匹配元素')

    例子:

    public decimal this[int month] {
        get {
            return _someArray[month];
        }
        set {
            _someArray[month] = value;
        }
    }
    
    public decimal this[int front, int month] {
        get {
            return _someArray2D[front - 1, month - 1];
        }
        set {
            _someArray2D[front - 1, month - 1] = value;
        }
    }
    

    几乎不可能追踪。我花了一整天的时间来反复试验。

    希望这对某人有所帮助。

    【讨论】:

      【解决方案3】:

      不是答案,而是提示。基于Tekerik JustDecompile,该方法看起来像(EntityFramework.dll v4.2.0.0):

      protected override EdmProperty MatchKeyProperty(EdmEntityType entityType, IEnumerable<EdmProperty> primitiveProperties)
      {
          return primitiveProperties.SingleOrDefault<EdmProperty>(delegate {
              return string.Concat(entityType.Name, "Id").Equals(p.Name, stringComparison);
          }) ?? primitiveProperties.SingleOrDefault<EdmProperty>(delegate {
              return string.Concat(entityType.Name, "Id").Equals(p.Name, stringComparison);
          });
      }
      

      这个函数似乎试图找出你的实体上的哪个属性是关键属性。它期待用户 - > UserId 映射。我无法解释为什么 SingleOrDefault 会被调用两次。

      【讨论】:

      • 它没有被调用两次。它使用三元(sp?)运算符。
      • @bdparrish,不,它使用空合并运算符。 (但你是对的,它没有调用两次)
      • 对不起不是叫两次,我的意思是同一个表情出现了两次。它由合并运算符分隔,但我找不到'??'两侧之间的任何不同操作员。代表似乎是一样的。
      • @PhilBolduc,真的,我也做不到。很好的收获。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多