Boom! InvalidOperationException ("Sequence contains no elements").
Now the befuddling aspect of this exception is where it was thrown. I basically have some code like:
using (var ctx = new DragonContext(db.CreateConnection())) { var unit = new SupplierPriceList() { SupplierPriceListID = "test", SupplierID = "foo", MaterialID = "foo", CompanyCode = "foo", Date = DateTime.Now, SupplierPrice = 1.23 }; ctx.SupplierPriceLists.Add(unit); ctx.SaveChanges(); }And the exception is thrown at the point of the call to Add(). Which doesn't seem to make a lot of sense as the temp database being used has no rows in any tables, so of course SupplierPriceLists is empty and one might expect that to be the source of the exception, considering the text, but it really shouldn't matter. Tables are empty all the time and we can add rows to them. Still, the error befuddles...
Looking at the stack trace, I see the last two frames are:
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)Ok, so the last frame explains the raw reason for the exception: something is doing a Single() call on an empty collection. But what? The call one up looks interesting:
GetStoreTypeFromName
seems to suggest that Entity is doing a lookup to find out how it should store the data for that property. So, on a hunch, comment out all properties except the implied key field (SupplierPriceListID) and the Add()
call works. Hm. Perhaps SQLCE doesn't like the double?fields? Nope -- uncomment them and things still work.
Then the culprit leaps out. The generator tool has annotated a
DateTime?
field with:
[Column(TypeName = "date")]Removing the annotation causes the error to subside -- and the
SaveChanges()
call on the context passes, on both MSSQL and SQLCEI thought I'd just make a record of this here as I found nothing particularly useful through much googling and eventually found and resolved the problem based on a hunch. Perhaps this post can save someone else a little mission. Of course the
DateTime?
value is truncated for the Date
field, but hey, that's what the client originally intended...
No comments:
Post a Comment