EF code-first: Sequence contains more than one matching element

c# ef-code-first entity-framework entity-framework-6

Question

I have an EF6 code-first project.User a class that requires connections to other users both inbound and outbound. I also have a connection, which has properties.Connections class.

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

The following error message appears when I update my database. I didn't initially have theInverseProperty properties, so I can see why EF was unsure about what to do in that situation. But even though the documentation makes it sound like these properties are what I want, it's still not working. But maybe I'm misinterpreting it.

I hope to acquire aUsers as well as aConnections a desk where theConnections has a tableID , SourceUserID , DestUserID (clearly with FK restrictions).

Any thoughts?

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, Func2 predicate) at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<>c__DisplayClass250.b__247(<>f__AnonymousType2b2 <>h__TransparentIdentifier242) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
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, Lazy1 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)
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

1
2
12/1/2014 9:47:55 PM

Accepted Answer

Sorry if using InverseProperties is required, however if you have the option to wire the relationships differently, you can accomplish this with the relationships.

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

then configurations must be added. To test this in my case, I did it in the context of a 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);
2
12/2/2014 12:56:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow