entity framework update child records
C#
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();
}
}
Also in C#:
- Title
- vector between two points unity
- Category
- C#
- Title
- how to find object by ag unity
- Category
- C#
- Title
- unity delete specific text in a string
- Category
- C#
- Title
- c# list length
- Category
- C#
- Title
- c# if statement
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- return a list of list from yaml via C#
- Category
- C#
- Title
- go to next itiration in foreach C#
- Category
- C#
- Title
- how to switch scenes in unity
- Category
- C#
- Title
- conditional compilation c#
- Category
- C#
- Title
- unity cast int to float
- Category
- C#
- Title
- isprime c#
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- .net core change localhost port
- Category
- C#
- Title
- c# pass mouse events to parent
- Category
- C#
- Title
- unity adding component to another gameobject
- Category
- C#
- Title
- generate range c#
- Category
- C#
- Title
- c# find comma in text and remove
- Category
- C#
- Title
- unity t-flip flop
- Category
- C#
- Title
- asp net identity add a unique fields to user
- Category
- C#
- Title
- create new object from generic c#
- Category
- C#
- Title
- unity getkey keycode
- Category
- C#
- Title
- how do i foreach c#
- Category
- C#
- Title
- c# changimg to one decimal place
- Category
- C#
- Title
- edit bool from diffirent script
- Category
- C#
- Title
- c# to binary
- Category
- C#
- Title
- or in C#
- Category
- C#
- Title
- asp.net core razor pages upload file
- Category
- C#
- Title
- c# Authorization has been denied for this request
- Category
- C#
- Title
- MVC company assignments
- Category
- C#
- Title
- c# prime factorization
- Category
- C#
- Title
- C# check many strings quickly
- Category
- C#
- Title
- eventos c#
- Category
- C#
- Title
- c# creating a data recovery software
- Category
- C#
- Title
- c# foreach arra
- Category
- C#
- Title
- asp list box
- Category
- C#
- Title
- unity rotate object relative to camera
- Category
- C#
- Title
- waitforseconds unity
- Category
- C#
- Title
- how to make rb.addforce 2d
- Category
- C#
- Title
- increase variable C#
- Category
- C#
- Title
- unity check when clicked on object
- Category
- C#
- Title
- downlaod file and use C#
- Category
- C#
- Title
- pass parameter to thread c#
- Category
- C#
- Title
- c# check if type implements interface
- Category
- C#
- Title
- The type or namespace name 'Scrollbar' could not be found
- Category
- C#
- Title
- c# add object to array
- Category
- C#
- Title
- unity left mouse button
- Category
- C#
- Title
- loop gridcontrol devexpress c#
- Category
- C#
- Title
- asp net mvc 5 return view from another controller
- Category
- C#
- Title
- how to get object to spawn in a curcle
- Category
- C#
- Title
- unity custom update
- Category
- C#
- Title
- c# linq join multiple conditions
- Category
- C#
- Title
- C# get all files in directory
- Category
- C#
- Title
- socket would block error c#
- Category
- C#
- Title
- void Update
- Category
- C#
- Title
- unity rotate around axis
- Category
- C#
- Title
- how to stop window from terminating c# visual studio
- Category
- C#
- Title
- get hash c#
- Category
- C#
- Title
- check if two timespans intersect c#
- Category
- C#
- Title
- c# type of generic is string
- Category
- C#
- Title
- c# sort array
- Category
- C#
- Title
- unity print
- Category
- C#
- Title
- inheritance c#
- Category
- C#
- Title
- c# array to list
- Category
- C#
- Title
- unity create gameobject
- Category
- C#
- Title
- c# calculate difference between two dates in days
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- asp textarea
- Category
- C#
- Title
- convert base64 string to string c#
- Category
- C#
- Title
- debug.log unity
- Category
- C#
- Title
- out parameters c#
- Category
- C#
- Title
- c# get every point in a line in matrix
- Category
- C#
- Title
- Exception thrown: 'System.FormatException' in mscorlib.dll dates
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- how to create a list in c# unity
- Category
- C#
- Title
- how to generate random unique id in c#
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- vector3.lerp
- Category
- C#
- Title
- simple reset transform.rotation c#
- Category
- C#
- Title
- c# read excel file
- Category
- C#
- Title
- custom player spawner mirror
- Category
- C#
- Title
- for each textbox c#
- Category
- C#
- Title
- c# remove last character from string
- Category
- C#
- Title
- unity onclick addlistener
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- c# how to print
- Category
- C#
- Title
- c# events and delegates
- Category
- C#
- Title
- validating file upload asp.net core mvc
- Category
- C#
- Title
- unity remove gameobject
- Category
- C#
- Title
- switch case c# contains
- Category
- C#
- Title
- debug.log
- Category
- C#
- Title
- clear combobox c#
- Category
- C#
- Title
- c# exit program
- Category
- C#
- Title
- move character unity
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- how to run csharp in visual studio code
- Category
- C#
- Title
- unity random int
- Category
- C#
- Title
- generics in c#
- Category
- C#
- Title
- C# string is all zeros
- Category
- C#
- Title
- error CS0542
- Category
- C#