gpt4 book ai didi

c# - EF 代码优先 : Sequence contains more than one matching element

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:13 25 4
gpt4 key购买 nike

我正在使用 EF6 代码优先,并且有一个 User需要与其他用户建立传入和传出连接的类。连接也有属性,所以我也有一个 Connections类。

public class User
{
public int ID { get; set; }

// Other properties removed here to keep it simple

[InverseProperty("SourceUser")]
public virtual ICollection<Connection> OutgoingConnections { get; set; }

[InverseProperty("DestUser")]
public virtual ICollection<Connection> IncomingConnections { get; set; }
}

public class Connection
{
public int ID { get; set; }

// Other properties removed here to keep it simple

[InverseProperty("OutgoingConnections")]
public User SourceUser { get; set; }

[InverseProperty("IncomingConnections")]
public User DestUser { get; set; }
}

更新数据库时出现以下错误。最初我没有 InverseProperty属性,所以我可以理解为什么 EF 在这种情况下不知道该怎么做。文档听起来这些属性是我想要的 - 但它仍然无法正常工作。也许我误会了。

我想以 Users 结尾表和一个 Connections Connections 所在的表表有 ID , SourceUserID , DestUserID (显然有 FK 约束)。

有什么想法吗?

Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201411192045091_InitialCreate]. Applying explicit migration: 201411192045091_InitialCreate. System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source,
Func
2 predicate) at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass250.b__247(<>f__AnonymousType2b2
<>h__TransparentIdentifier242) at
System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()<br/>
at System.Collections.Generic.List
1..ctor(IEnumerable1 collection)<br/>
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy1 modificationCommandTreeGenerator,
MigrationSqlGenerator migrationSqlGenerator, String
sourceModelVersion, String targetModelVersion) at
System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument
sourceModel, XDocument targetModel, Lazy
1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion) at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1
pendingMigrations, String targetMigrationId, String lastMigrationId)<br/>
at
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.b__b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Sequence contains more than one matching element

最佳答案

抱歉,如果需要使用 InverseProperties,但如果您可以灵活地以不同方式连接它们,则可以通过这种方式完成关系。

public class User
{
public int ID { get; set; }

[ForeignKey("ID")]
public virtual ICollection<Connection> OutgoingConnections { get; set; }

[ForeignKey("ID")]
public virtual ICollection<Connection> IncomingConnections { get; set; }
}

public class Connection
{
public int ID { get; set; }

public int SourcerId {get;set;}

public int DestUserId {get;set;}

[ForeignKey("SourcerId")]
public User SourceUser { get; set; }

[ForeignKey("DestUserId")]
public User DestUser { get; set; }
}

然后你需要添加配置。在我的例子中,为了测试这个,我在上下文中的 protected override void OnModelCreating(DbModelBuilder modelBuilder) 中做了它。

    modelBuilder.Entity<User>().HasMany(x=>x.IncomingConnections).WithRequired(x=>x.SourceUser).WillCascadeOnDelete(false);
modelBuilder.Entity<User>().HasMany(x => x.OutgoingConnections).WithRequired(x => x.DestUser).WillCascadeOnDelete(false);

关于c# - EF 代码优先 : Sequence contains more than one matching element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27237374/

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