Using EF 4.1, create a single "Unit of Work" from a parent record and numerous child records.

c# entity-framework

Question

I am practicing with a code-first set of classes with a parent-child relationship between them.

public class Parent
{
    public int Id ( get; set; }
    public string Name { get; set; }
    public List<Child> Children { get; set; }

    public Parent()
    {
        Children = new List<Child>();
    }
}

public class Child()
{
    public int Id ( get; set; }
    public string Name { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }
}

In a simple console app I create a parent and create two child records and add them to the parent. If I then call the SaveChanges() only the first child is added to the children table.

var x = new MyContext();

var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };

var p = new Parent {Name = "Alpha-Parent"};

p.Children.Add(c1);
p.Children.Add(c2);

x.Parents.Add(p);

x.SaveChanges();

Well that was not what I wanted, so I tried to maybe add then to both the parent object and the context object.

var x = new MyContext();

var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };

var p = new Parent {Name = "Alpha-Parent"};

p.Children.Add(c1);
p.Children.Add(c2);

x.Children.Add(c1);
x.Children.Add(c2);

x.Parents.Add(p);

x.SaveChanges();

That got both the child records into the database, but only one of them (the first) was associated with the parent record.

Last, but not least, I tried to do a call to SaveChanges() after each call to Parent.Add().

var x = new MyContext();

var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };

var p = new Parent {Name = "Alpha-Parent"};

x.Parents.Add(p);

p.Children.Add(c1);
x.SaveChanges();

p.Children.Add(c2);
x.SaveChanges();

But, this breaks my idea of how the "Unit of Work" Pattern works. I "Should be able to call SaveChanges() once, and have all my changes take place, correct?

So, what am I doing wrong?

1
8
2/20/2012 2:56:04 AM

Accepted Answer

The following piece of code will save in single save changes. And always use a transactionscope to save in single batch.

public class Child 
{
  public int Id ( get; set; }
  public string Name { get; set; }
  public Parent Parent {set;get;}
}


var x = new MyContext();
var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };

var p = new Parent {Name = "Alpha-Parent"};
c1.Parent = p;
c2.Parent = p;
x.Parents.Add(p);
x.SaveChanges();
8
2/20/2012 7:31:14 AM

Popular Answer

I don't know why you inherit from EntityBase since you mentioned that you are testing with POCO Code First. Anyway, I tested following code with success

 class Program
    {
        static void Main(string[] args)
        {
            var x = new MyContext();

            var c1 = new Child { Name = "Alpha-Child" };
            var c2 = new Child { Name = "Beta-Child" };

            var p = new Parent { Name = "Alpha-Parent" };

            p.Children.Add(c1);
            p.Children.Add(c2);

            x.Parents.Add(p);

            x.SaveChanges(); 


            Console.Read();
        }
    }

   public class Parent 
   { 
        public Parent() 
        { 
            Children = new List<Child>(); 
        } 

       public int Id { get; set; }
        public string Name { get; set; } 
        public List<Child> Children { get; set; } 
    } 

    public class Child
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
    } 

    public class MyContext : DbContext 
    { 
        public DbSet<Parent> Parents { get; set; } 
        public DbSet<Child> Children { get; set; } 
    } 


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