Horje
entity framework update child records Code Example
entity framework update child records
public void Update(UpdateParentModel model)
{
    var existingParent = _dbContext.Parents
        .Where(p => p.Id == model.Id)
        .Include(p => p.Children)
        .SingleOrDefault();

    if (existingParent != null)
    {
        // Update parent
        _dbContext.Entry(existingParent).CurrentValues.SetValues(model);

        // Delete children
        foreach (var existingChild in existingParent.Children.ToList())
        {
            if (!model.Children.Any(c => c.Id == existingChild.Id))
                _dbContext.Children.Remove(existingChild);
        }

        // Update and Insert children
        foreach (var childModel in model.Children)
        {
            var existingChild = existingParent.Children
                .Where(c => c.Id == childModel.Id)
                .SingleOrDefault();

            if (existingChild != null)
                // Update child
                _dbContext.Entry(existingChild).CurrentValues.SetValues(childModel);
            else
            {
                // Insert child
                var newChild = new Child
                {
                    Data = childModel.Data,
                    //...
                };
                existingParent.Children.Add(newChild);
            }
        }

        _dbContext.SaveChanges();
    }
}




Csharp

Related
random.range unity not working Code Example random.range unity not working Code Example
Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager Code Example Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager Code Example
Unity c#loading a scene after a few seconds Code Example Unity c#loading a scene after a few seconds Code Example
c# array remove first element Code Example c# array remove first element Code Example
C# 1 minute delay Code Example C# 1 minute delay Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11