0

i am encountering a scenario that is as follows.

i have two tables named parent and child.

Upon fetching data from database, i got 1 row against parent and 2 rows against child.

At this stage if i ​create new child and add it to context and apply save changes, so ​now my database contains 1 row against parent and 3 rows against child but my context only contains 2 rows against child...

Do i need to refresh whole context or only that child to get latest database childs record in context so that i can do other operations with newly added child during the entire operation.

Parent objparent = context.Parent( p => p.id = 4);
objparent.count(); //1
objparent.Child.count();//2

Child objchild = new Child();
objchild.name = "abc";
objchild.parentid = 4;

context.add(objchild);
context.savechanges();
//do i need to refresh whole context to have newly added child under given parent
//or i should only fetch all childs against parent id 4 and re-attach them to context
tango
  • 80
  • 2
  • 11
  • You should use managed navigation properties in this scenario http://docs.telerik.com/data-access/developers-guide/crud-operations/manage-navigation-properties/developer-guide-manage-navigation-properties-overview – Damyan Bogoev Oct 21 '15 at 15:07
  • @DamyanBogoev: Hi sir thanks for the reply, yes i am already using managed navigation property. i was asking if i do savechanges here, how will i update my context with that newly entered record i.e before update child as 2 rows within the context, after update i wanted to have 3 rows within that context – tango Oct 21 '15 at 15:25
  • shall i write objParent.childs = context.Child(c=>c.parentID = 4); to populate given context with newly entered record – tango Oct 21 '15 at 15:28
  • You could directly add the child object to the parent child collection and commit the changes to the database: Child objchild = new Child(); objchild.name = "abc"; objparent.Child.Add(objchild); context.SaveChanges(); – Damyan Bogoev Oct 21 '15 at 17:39
  • Setting the parent id and storing the child object in the database should work as well. It looks like a misconfiguration of your metadata regarding the parent - child association. – Damyan Bogoev Oct 21 '15 at 17:43
  • Hi Sir, i have noticed one thing, the moment new record gets saved in database by invoking context.savechanges(), it will automatically be fetched by current context along with its identity id (sequence) and had state as 'managed loaded' – tango Oct 26 '15 at 08:33
  • Telerik DataAccess generates an insert statement with a suffix, which gets the auto-incremented value (if possible) and assigns it to the corresponding property later on. – Damyan Bogoev Oct 26 '15 at 11:31

1 Answers1

0

For future readers, I wanted to mark this question as solved, thanks Damyan Bogoev for his guidance.

I have observed entity framework functionality that is the moment new record gets saved in database by invoking context.savechanges(), it will automatically be fetched by current context along with its identity id (sequence / newly inserted primary key) and had state set as 'managed loaded'

Means i can use newly inserted row within current context without re-fetching / refreshing my context.

tango
  • 80
  • 2
  • 11