New Posts New Posts RSS Feed: Error when saving entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Error when saving entity

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: Error when saving entity
    Posted: 22-Jul-2010 at 7:19am
This will also happen if you have a datetime as part of a primary key in SQLServer.  The milliseconds value will be changed when the record is stored due to the accuracy being rounded to 0.000, 0.003 or 0.007 (depending on your setting).  Just trim off the milliseconds.
Back to Top
dpollot44 View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Jan-2010
Location: Rochester NY
Posts: 24
Post Options Post Options   Quote dpollot44 Quote  Post ReplyReply Direct Link To This Post Posted: 12-Jul-2010 at 7:12am
Thanks.  That was indeed the problem (however, since we're pointing to an informix database, the char field is not necessarily 10.  In my case it was 12, but it'll wind up being whatever it was defined to be).
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 09-Jul-2010 at 3:29pm
Here was the resolution to Nitin's problem:
 
The problem is that your entity's database key is a char type, which is always going to be a fixed length of 10. When you save a new instance with a key value like "Test4", that key gets converted to "Test4     " by the database. When the entity with key "Test4     " comes back to the Entity Framework (server-side), DevForce asks the EF to refresh the entities that were saved (so that they will include any changes made by triggers or such on the database, and thus will be accurate representations of what is in the database).  But the EF looks for an entity with key "Test4    " and doesn't find one, so it throws an exception.

There are two possible work-arounds:

1. Use an NCHAR key instead of CHAR so the database will accept the less-than-10-character value as is; OR
2. Ensure that the value you give to a new entity for its key property is exactly 10 characters; .e.g.,
         table.UserName = ("Test" + number).PadRight(10);

It is possible to set DevForce (using a SaveOption) so that it does not order a refresh of the saved entity; but your use case actually represents a good example of why that's not a good idea. If DevForce weren't doing the refresh, you wouldn't know that you local entity had ended up, after the save, with a different key value than its corresponding database record.  And that would make it impossible for you to update the record.
Back to Top
dpollot44 View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Jan-2010
Location: Rochester NY
Posts: 24
Post Options Post Options   Quote dpollot44 Quote  Post ReplyReply Direct Link To This Post Posted: 09-Jul-2010 at 12:11pm
Has there been any answer to this?
I'm running into the same error...

Suppose that we have two tables (and I'm just going to make up an example to protect the innocent) Libraries and Books.  Libraries, within the entity data model will wind up having a conceptual model type of Library, and Books will have Book.  A Library contains an identity column on it called Id, and likewise a Book has the same.  Library also has a property/column LibraryCode which is a string (so from the store perspective - char/varchar/whatever).  Book does NOT have a LibraryId integer column mapping it back to the Library, however it DOES have a LibraryCode property/column which is of the same char/varchar/whatever type property/column on the Library object.  Now imagine that we decide to be unconventional in our construction of our Entity Model, and we decide that our Library's primary key will be "LibraryCode".  We'll use "LibraryCode" to connect our two entities together - thereby creating a "Books" navigation property on Library, and a "Library" navigation property on Book.  (Let's assume that in my world, a book can belong to at most one library.)

This situation seems to allow me to successfully load both Libary and Book entities from the data store, however, when I try to create a Library, it saves just fine but blows up in the end with error that's described in the previous post...

Any ideas?
Back to Top
ngoel View Drop Down
Newbie
Newbie


Joined: 17-Nov-2009
Posts: 15
Post Options Post Options   Quote ngoel Quote  Post ReplyReply Direct Link To This Post Posted: 24-May-2010 at 7:06pm
Hi
 
I am creating an Entity and saving it to the database using:
 
EntityManager.SaveChanges();
 
But, it is returning with the following error. The Entity is getting saved to the database but after it has saved the Entity to the database, it seems to be doing some refresh and failing.
 
IdeaBlade.EntityModel.EntityManagerSaveException occurred
  Message=The refresh attempt has failed because an unexpected entity was returned by the data source.
  Source=IdeaBlade.EntityModel
  Cancelled=false
  StackTrace:
       at IdeaBlade.EntityModel.EntityManager.HandleSaveResultException(SaveWorkState saveWorkState, Boolean isAsyncOp)
       at IdeaBlade.EntityModel.EntityManager.SaveChangesCore(IEnumerable entities, SaveOptions saveOptions)
       at IdeaBlade.EntityModel.EntityManager.SaveChanges(IEnumerable entities, SaveOptions saveOptions)
       at IdeaBlade.EntityModel.EntityManager.SaveChanges()
       at Microster.Maintenance.Views.OrgLevelSecurityPresenter.CreateOrganisationItemAccessEntity(HierarcialItem orgItem)
  InnerException: System.InvalidOperationException
       Message=The refresh attempt has failed because an unexpected entity was returned by the data source.
       Source=System.Data.Entity
       StackTrace:
            at System.Data.Objects.ObjectContext.BatchRefreshEntitiesByKey(RefreshMode refreshMode, Dictionary`2 trackedEntities, EntitySet targetSet, List`1 targetKeys, Int32 startFrom)
            at System.Data.Objects.ObjectContext.RefreshEntities(RefreshMode refreshMode, IEnumerable collection)
            at System.Data.Objects.ObjectContext.Refresh(RefreshMode refreshMode, IEnumerable collection)
            at IdeaBlade.EntityModel.Edm.EdmSaveHelper.RefreshEntities(IEnumerable`1 groupsByType)
            at IdeaBlade.EntityModel.Edm.EdmSaveHelper.SaveWithinContext()
            at IdeaBlade.EntityModel.Edm.EdmSaveHelper.Save()
       InnerException:
 
 
Thanks
Nitin
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down