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

c# entity-framework

Question

I am experimenting with a set of parent-child classes that are code-first classes.

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; }
}

I generate two kid records and add them to the parent in a simple terminal program. Only the first child is added to the children table if I then run SaveChanges().

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();

I attempted to maybe add them to both the parent object and the context object since it was not what I intended.

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();

Due to this, the database received both of the kid records, but only the first one of them was linked to the parent record.

Not least of all, I attempted to call 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();

However, this contradicts how I see the "Unit of Work" Pattern to operate. 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 code below will only save one update at a time. And to always save in a single batch, use a transactionscope.

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

Given that you stated testing POCO Code First, I'm not sure why you inherit from EntityBase. However, I successfully tried the following code.

 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