0
class Parent
{
    public int id;
    public ICollection<Child> Children;
}

class Child
{
    public int id;
    public int childProperty1;
    public decimal? childProperty2;
    public string childProperty3;
    public Parent parent;
}

My code for attaching child entity to dbContext:

// Getting the DbContext
            DatabaseContext dbContext = dbContextScope.DbContexts.Get<DatabaseContext>();

            // Attaches the modified entity to context in disconnected architecture.
            dbContext.Parent.Attach(this.parent);
            dbContext.Entry(this.parent).State = EntityState.Modified;

            this.parent.children.Select(s => dbContext.Entry(s).State = EntityState.Modified);

            // SaveChanges
            await dbContextScope.SaveChangesAsync().AnyContext();

Update: Added this piece of code insted of the .Select():

foreach(var childEntity in this.parent.Children)
{
    dbContext.Child.Attach(childEntity);
    dbContext.Entry(childEntry).State = EntityState.Modified;
}

My dbContext is in disconnected state. I have to save the parent class and a collection of child class records say 5 child class records to the database. How can I achieve that? I have gone through these links:

update disconnected object in entity framework

Updating collection in EF

Storing a complex detached object graph in EF6

0 Answers0