Hi guys, I am using EF in my application. Initially, the database wasn't created yet in the DB server, instead my application will create it based on my model. So here's the problem, when I run the program, I'm encountering this error
Count '_temp.Count' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
Sequence Contains more than one element.
I need your help guys. Thanks in advance.
boycotoPosted Aug 16, 2012, 5:16 AM
public virtual TObject Create(TObject TObject)
{
var newEntry = DbSet.Add(TObject);
return newEntry;
}
public virtual int Commit()
{
if (!shareContext)
return _database.SaveChanges();
return 0;
}
To insert the record to the database from csv
try
{
foreach(Employee ep in profile)
{
_employeeData.Create(ep);
}
_employeeData.Commit();
}
catch(Exception ex)
{
System.Diagnostics.Debug.Write(ex.StackTrace);
}
VulpesPosted Aug 16, 2012, 4:47 AM
So, if you just want to get one element, use First() instead.
boycotoPosted Aug 16, 2012, 4:38 AM
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GetQualifiedTableName(XDocument model, String entitySetName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.
at System.Linq.Enumerable.
at System.Linq.Enumerable.
at System.Linq.Enumerable.
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
at System.Data.Entity.Internal.InternalContext.ModelMatches(XDocument model)
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at UpdateRepository`1.Create(TObject TObject) in
boycotoPosted Aug 16, 2012, 4:12 AM
Nattudurai EswaramurthyPosted Aug 16, 2012, 2:08 AM
Single and SingleOrDefault are designed to throw if more that one match exists in the sequence. A consequence of this is that the entire sequence must be iterated prior to completion. It does not sound like this is what you want. Try FirstOrDefault instead:
This will (generally) perform better because it completes as soon as a match is found.
Please mark this answer as accepted answer if it resolves your problem.