gpt4 book ai didi

c# - FirstOrDefault 抛出 'Sequence contains more than one matching element'

转载 作者:行者123 更新时间:2023-11-30 14:15:51 25 4
gpt4 key购买 nike

我已经看到大多数人在使用 SingleOrDefault 时遇到此错误。但是,我正在使用 FirstOrDefault。有没有人见过这个异常?我正在使用存储库模式以使用依赖注入(inject)。

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

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

最佳答案

如果有人担心答案...

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

基础实体.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);
}
}
}

关于c# - FirstOrDefault 抛出 'Sequence contains more than one matching element',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com