How to update an expression tree with GraphDiff

c# entity-framework graphdiff

Question

Basically, the object graph I'm working with is

public class Resource
    {
        public string Forename { get; set; }
        public string Surname { get; set; }
        public int EmployeeNumber { get; set; }
        public ICollection<Skill> Skills { get; set; }
    }
public class Skill
    {
        public int SkillId{get; private set;}
        public Technology Technology { get; set; }
        public SkillCategory Category { get; set; }
        public SkillCompetency Competency { get; set; }    

    }

By choosing from the available Technology, SkillCategory, and SkillCompetency combinations, a new skill can be added to the user's repertoire. I've been attempting to utilize GraphDiff to stop EF from trying to add duplicate Technology, SkillCategory, and SkillCompetency records, but I've been failing miserably at it. This seems like it should be easy to accomplish with GraphDiff, but I'm having trouble because I'm quite new to EF and have only recently discovered GraphDiff.

Any thoughts?

1
2
5/29/2014 4:38:10 PM

Accepted Answer

Zzz-13-Zzz can be understood as "being a part of," which means that anything that is owned will be inserted, updated, or deleted along with its owner. GraphDiff fundamentally identifies two types of relations: owned and associated. The other relation type that GraphDiff handles is associated, which indicates that when updating a graph, GraphDiff only modifies relations to the related entities—not the entities themselves.

Returning to your example, you don't want repeatedTechnology , Category or Competency but entitiesSkills simply combine those, thus they are redundant.Skills are good. In order to model this with GraphDiff, you instruct it to take intoSkills as components ofResource (A zzz-46 zzzResource ) andTechnology , Category and Competency as a result of aSkill . This is depicted as follows:

// these three entities are all managed separately and have already been saved elsewhere
Technology entityFrameworkCodeFirst;
Category objectRelationalMappers;
Competency notThatIncompetent;

using (DbContext context = new WhatEverYourContextIsNamed())
{
    Resource developer = new Resource
    {
        Skills = new List<Skill> 
        {  
            new Skill
            {
                Technology = entityFrameworkCodeFirst,
                Category = objectRelationalMappers,
                Competency = notThatIncompetent,
            }
        } 
    };
    context.UpdateGraph(developer, 
        map => map.OwnedCollection(r => r.Skills, 
            with => with.AssociatedEntity(skill => skill.Technology)
                        .AssociatedEntity(skill => skill.Category)
                        .AssociatedEntity(skill => skill.Competency)));
    context.SaveChanges();
}
2
3/27/2014 8:17:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow