简体   繁体   中英

ASP.NETSequence contains more than one element

I have 3 very simple classes one for Category , one for products and one for Images After Migration and try to update-database but and the end of the process I get a

Sequence contains more than one element

the whole thing is below but notice that it says something about line 73 of the Configuration Class. Here is a fragment of that Class containing at the end, that line

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();

And herewhat shows after I try to update-database

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

I can not see/understand where/what is the error thanks

You are getting duplicates with your AddOrUpdate. Seems like you should be using ID as the unique key:

imagenes.ForEach(m => context.Images.AddOrUpdate(x => x.ID, m));
context.SaveChanges();

Also, make sure you clear out any conflicting records from the Images table.

http://thedatafarm.com/data-access/take-care-with-ef-4-3-addorupdate-method/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM