我有3个非常简单的类,一个用于类别,一个用于产品,一个用于图像 迁移后尝试更新数据库,但是在流程结束时我得到了
Sequence contains more than one element
整个事情在下面,但请注意它说明了配置类的第73行。 这是该类的一个片段,最后包含该行
var imagenes = new List<Image>
{
new Image{ ID = 1, ImageName = "Horizontal_Faux_Wood", ImagePath="Content/ProductImages/fauxwoodblinds1.jpg", ProductID = 1},
new Image{ ID = 2, ImageName = "Horizontal_Real_Wood", ImagePath="Content/ProductImages/woodblinds1.jpg", ProductID = 2},
new Image{ ID = 3, ImageName = "Roller_Shade_Screens", ImagePath="Content/ProductImages/product-rollupshades-a-5p.jpg", ProductID = 3},
new Image{ ID = 4, ImageName = "BlackOut", ImagePath="Content/ProductImages/product-rollupshades-9-blackout_soft_side channel.jpg", ProductID = 4},
new Image{ ID = 5, ImageName = "Vertical_Faux_Wood", ImagePath="Content/ProductImages/verticalfauxwood.jpg", ProductID = 5},
new Image{ ID = 6, ImageName = "Vertical_PVC_Textured", ImagePath="Content/ProductImages/woodblindspeq.jpg", ProductID = 6},
new Image{ ID = 7, ImageName = "Vertical_PVC_Plain", ImagePath="Content/ProductImages/vertical.jpg", ProductID = 7},
};
(line 73) imagenes.ForEach(m => context.Images.AddOrUpdate(x => x.ImagePath, m)); context.SaveChanges();
在我尝试更新数据库
后,这里显示了什么PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
System.InvalidOperationException: Sequence contains more than one element
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](DbSet`1 set, IEnumerable`1 identifyingProperties, InternalSet`1 internalSet, TEntity[] entities)
at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet`1 set, Expression`1 identifierExpression, TEntity[] entities)
at AllBlindsNewWeb.Migrations.Configuration.<>c__DisplayClass1_0.<Seed>b__2(Image m) in C:\Users\Ernesto\Documents\Visual Studio 2015\Projects\training\AllBlindsNewWeb\AllBlindsNewWeb\Migrations\Configuration.cs:line 73
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at AllBlindsNewWeb.Migrations.Configuration.Seed(ProductContext context) in C:\Users\Ernesto\Documents\Visual Studio 2015\Projects\training\AllBlindsNewWeb\AllBlindsNewWeb\Migrations\Configuration.cs:line 73
at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 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.<Update>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 element
我无法看到/了解错误的位置/错误 感谢
答案 0 :(得分:0)
您正在使用AddOrUpdate获取重复项。好像你应该使用ID作为唯一键:
imagenes.ForEach(m => context.Images.AddOrUpdate(x => x.ID, m));
context.SaveChanges();
另外,请确保清除Images表中的任何冲突记录。