New Posts New Posts RSS Feed: SaveChangesAsync - entity with it's related entities
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

SaveChangesAsync - entity with it's related entities

 Post Reply Post Reply
Author
redman View Drop Down
Newbie
Newbie


Joined: 14-Jun-2012
Posts: 4
Post Options Post Options   Quote redman Quote  Post ReplyReply Direct Link To This Post Topic: SaveChangesAsync - entity with it's related entities
    Posted: 21-Jun-2012 at 12:37am
It was a save interceptor problem :)
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 15-Jun-2012 at 12:38pm
redman,
 
If you are doing a partial save, you will need to include all entities to be saved. What I was initially referring to was that you are deliberately changing EntityAspect from Detached to Added. You do not need to to that as DevForce will automatically update the EntityState if you associate entity A (EntityState.Detached) to entity B (EntityState.Added/Unchanged).
 
 
Let's go back a bit... you mentioned your code 'fails with error "The INSERT statement conflicted with the FOREIGN KEY constraint ...."'.
 
I need to know what are the 2 entity type names you are saving AND the complete error message. (i.e. including the types involved in the constraint)
 
Regards,
   Silvio.
Back to Top
redman View Drop Down
Newbie
Newbie


Joined: 14-Jun-2012
Posts: 4
Post Options Post Options   Quote redman Quote  Post ReplyReply Direct Link To This Post Posted: 14-Jun-2012 at 11:19pm
So you say that this should save related entities too?

EntityManager.SaveChangesAsync(new List<Entity>(){ MyParentEntitie }, ....)


If I don't add related entities to list of entities that should be saved it fails with:

An entity or entities containing a referenced temporary Id is missing from the list of entities provided. Missing entities include: MyEntityType: -101.  See exception members for more details

But when I add them manually it inserts them in DB and still throws error (that's interesting, because it actually inserts relations into DB) on FK.

I removed FK and it goes like this:
It first saves ParentEntity ok, and then it saves related entities, but it tries twice for each. First (I guess it's first, because it should fail otherwise on first error) it saves with the right foreign key, and then tries to save it again with -100 for foreign key (this is internal for ParentEntity). Now I have two questions:
1. Why it tries to save it twice?
2. Shouldn't that SaveChanges work in a transaction? How can than happen that something is saved when error occurrs?

Edit: the above only happens when adding, when updating it works like expected


Edited by redman - 15-Jun-2012 at 12:28am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 14-Jun-2012 at 2:42pm
Hi redman,
 
First of all, I assume you are writing this code as a form of updating the many-to-many related entities once they are "linked" to the existing entity (i.e. Model).
Note that DevForce should be doing this automatically for you. (assuming your Model entity is Added or Unchanged)
 
Back to your code, I don't see anything wrong with it. (besides dynamic pval = Model.GetPropertyValue(prop.Name); - where I use dynamic pval = entity.EntityAspect.GetValue(prop.Name) instead)
 
While I was testing it (using Employee/Territory many-to-many relation fom NorthwindIB) I got the same error, but the issue was not between Employee and Territory. The issue was between Territory and Region as their DB constraint was not being satisfied.
 
I'm  guessing your issue is the same and the constraint is probably not between the 2 entity types being saved.
 
Let me know.
 
Regards,
   Silvio.


Edited by sbelini - 14-Jun-2012 at 3:07pm
Back to Top
redman View Drop Down
Newbie
Newbie


Joined: 14-Jun-2012
Posts: 4
Post Options Post Options   Quote redman Quote  Post ReplyReply Direct Link To This Post Posted: 14-Jun-2012 at 5:41am
Hi,

I have an entity that has some many to many relationships. I get related entities like this:

private IEnumerable<Entity> GetRelatedEntities()
{
       //Model = this is an entity

var entities = new List<Entity>() { Model };
var t = Model.GetType();
var props = t.GetProperties().Where(p =>
  {
  var ptype = p.PropertyType;

  if (!ptype.IsGenericType) return false;

var gtype = ptype.GetGenericTypeDefinition();
if (gtype == typeof(RelatedEntityList<>))
{
return true;
}

  return false;
  }).ToList();

foreach (var prop in props)
{
dynamic pval = Model.GetPropertyValue(prop.Name);

foreach (dynamic e in pval)
{
if (e.EntityAspect.EntityState == EntityState.Detached)
{
DomainContext.AddEntity(e);
}
entities.Add(e);
}
}

return entities;
}

But when I save this list (EntityManager.SaveChangesAsync(GetRelatedEntities(),....) it fails with error "The INSERT statement conflicted with the FOREIGN KEY constraint ....". What I'm doing wrong?

p.s.: this is done in a ViewModelBase which is a generic class. I can't just do EntityRelations.XYZ and then FindEntityGraph, I have to do this at runtime, for "unknown" entity.


Edited by redman - 14-Jun-2012 at 5:46am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down