Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Entity framwork update parent entity added new sub entity

//Interests
//Delete children
foreach (var existingChild in dbProfile.Interests.ToList())
{
    if (!profile.Interests.Any(c => c.Name == existingChild.Name))
        db.Interests.Remove(existingChild);
}

//Update and Insert children
foreach (var childModel in profile.Interests)
{
    var existingChild = dbProfile.Interests
        .Where(c => c.Name == childModel.Name)
        .SingleOrDefault();

    if (existingChild != null)
    {
        // Update child
        childModel.Id = existingChild.Id;
        db.Entry(existingChild).CurrentValues.SetValues(childModel);
    }
    else
    {
        // Insert child
        var newChild = new Interest
        {
            Name = childModel.Name,
        };
        dbProfile.Interests.Add(newChild);
    }
}
Comment

Entity framwork update parent entity added new sub entity

protected void UpdateChildCollection<Tparent, Tid , Tchild>(Tparent dbItem, Tparent newItem, Func<Tparent, IEnumerable<Tchild>> selector, Func<Tchild, Tid> idSelector) where Tchild : class
    {
        var dbItems = selector(dbItem).ToList();
        var newItems = selector(newItem).ToList();

        if (dbItems == null && newItems == null)
            return;

        var original = dbItems?.ToDictionary(idSelector) ?? new Dictionary<Tid, Tchild>();
        var updated = newItems?.ToDictionary(idSelector) ?? new Dictionary<Tid, Tchild>();

        var toRemove = original.Where(i => !updated.ContainsKey(i.Key)).ToArray();
        var removed = toRemove.Select(i => DbContext.Entry(i.Value).State = EntityState.Deleted).ToArray();

        var toUpdate = original.Where(i => updated.ContainsKey(i.Key)).ToList();
        toUpdate.ForEach(i => DbContext.Entry(i.Value).CurrentValues.SetValues(updated[i.Key]));

        var toAdd = updated.Where(i => !original.ContainsKey(i.Key)).ToList();
        toAdd.ForEach(i => DbContext.Set<Tchild>().Add(i.Value));
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: How to find column name with column index in DataGridView 
Csharp :: rotate skybox on x axis unity 
Csharp :: blazor use static json files 
Csharp :: c# how to refresh input field 
Csharp :: How to remove an element from Array List in C#? 
Csharp :: why does everything reset when switching scene unity 
Csharp :: scaffolding in vs22 asp.net 6 
Csharp :: jq map over array 
Csharp :: .net 3.1 bind json config 
Csharp :: discord bot c# how to refresh message 
Csharp :: no cameras rendering unity 
Csharp :: Bedingungen in C# – if, else und else if 
Csharp :: List of border roleplays roblox 
Csharp :: clear highlight winforms treeview 
Csharp :: Xamarin forms XAML change value 
Csharp :: Working with null values 
Csharp :: C# Action Delegate 
Csharp :: c# UserControl make background transparent 
Csharp :: mvc input number rounding 
Csharp :: c# generate random date of birth but over 18 
Csharp :: C# Search in JSON without deserialization 
Csharp :: principalcontext c# example 
Csharp :: best unity regex for email validation in c# 
Csharp :: how to delete dotnet project 
Csharp :: entity framework get all 
Csharp :: how to check that a gameobject touches a colour in unity c# 
Csharp :: DataTable GetErrors 
Csharp :: add two large numbers 
Csharp :: dictionary and generic class c# 
Csharp :: asp.net disabled checkbox style 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =