I'm using EF6 code-first, and have a User
class that needs to have incoming and outgoing connections to other users. A connection also has properties, so I also have a 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; }
}
I'm getting the following error when updating my database. Initially I didn't have the InverseProperty
attributes, so I can understand why EF didn't know what to do in that case. The documentation sounds like these attributes are what I want though - but it's still not working. Perhaps I'm misunderstanding it though.
I want to end up with a Users
table, and a Connections
table where the Connections
table has ID
, SourceUserID
, DestUserID
(obviously with FK constraints).
Any ideas?
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](IEnumerable
1 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()
1..ctor(IEnumerable
at System.Collections.Generic.List1 collection)
1 source) at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 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)
1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
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