切换到HasMany()并获取Sequence不包含匹配元素错误

时间:2017-07-20 23:26:50

标签: c# .net entity-framework linq ef-code-first

我在一个项目中,实体使用额外的类来映射它们之间的关系,例如:

public class MainEnt 
{
    public int Id { get; set; }
    public string Description { get; set; }

    public ICollection<MainEntAnotherEnt> AnotherEnts{get;set;}
}

public class AnotherEnt 
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class MainEntAnotherEnt
{
    public int Id { get; set; }
    public int MainEntId{ get; set; }
    public int AnotherEntId{ get; set; }
}

该项目还使用自动迁移,因此在其中一次迁移中显式创建了MainEntAnotherEnt表。

        CreateTable(
               "dbo.MainEntAnotherEnt",
               c => new
               {
                   Id = c.Int(nullable: false, identity: true),
                   MainEntId = c.Int(nullable: false),
                   AnotherEntId = c.Int(nullable: false),
               })
               .PrimaryKey(t => new { t.Id })
               .ForeignKey("dbo.AnotherEnt", t => t.AnotherEntId)
               .ForeignKey("dbo.MainEnt", t => t.MainEntId)
               .Index(t => t.MainEntId)
               .Index(t => t.AnotherEntId);

我想改变这种方法,因此我们摆脱了MainEntAnotherEnt类,我们可以使用映射来完成它,所以我做了这些更改:

public class MainEnt 
{
    public int Id { get; set; }
    public string Description { get; set; }

    public virtual ICollection<AnotherEnt> AnotherEnts{get;set;}
}

public class AnotherEnt 
{
    public int Id { get; set; }
    public string Description { get; set; }

    public virtual ICollection<MainEnt> MainEnt {get;set;}
}


   modelBuilder.Entity<MainEnt >()
            .HasMany(m => m.AnotherEnts)
            .WithMany(a => a.MainEnt)
            .Map(ma =>
            {
                ma.MapLeftKey("MainEntId");
                ma.MapRightKey("AnotherEntId");
                ma.ToTable("MainEntAnotherEnt");
            });

但是我收到了错误:

  

[InvalidOperationException:Sequence不包含匹配元素]      System.Linq.Enumerable.Single(IEnumerable`1 source,Func`2谓词)+472
     System.Data.Entity.Migrations.Infrastructure.d__70.MoveNext()+ 471
     System.Linq.d__22`3.MoveNext()+355
     System.Linq.d__63`1.MoveNext()+79
     System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)+504
     System.Linq.Enumerable.ToList(IEnumerable`1 source)+70

     System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.FindAssociationTypePairs(ICollection`1 entityTypePairs)+862
     System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source,ModelMetadata target,Lazy`1 modificationCommandTreeGenerator,MigrationSqlGenerator migrationSqlGenerator,String sourceModelVersion,String targetModelVersion)+222
     System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel,XDocument targetModel,Lazy`1 modificationCommandTreeGenerator,MigrationSqlGenerator migrationSqlGenerator,String sourceModelVersion,String targetModelVersion)+588
     System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model,DbMigration lastMigration)+72
     System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)+342
     System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)+609
     System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)+609
     System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)+116
     System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)+156
     System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)+71
     System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()+ 482
     System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)+174
     System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)+273
     System.Data.Entity.Database.Initialize(布尔力)+80

任何有关如何解决此问题的指导都将非常感谢!

0 个答案:

没有答案