Saving Many to Many relationship - Entity Framework

Viewed 251

I have the following classes:

    public class item
    {
        public int NodeID
        {
            get ;
            set ;
        }
        public int Weight
        {
            get ;
            set ;
        }
        public int Category
        {
            get ;
            set ;
        }
    }
   public class Recipients
    {
        public int RecipientID
        {
            get ; set;
        }
    }

    public class Nodes
    {
        public List<int> RecipientList
        {
            get ;
            set ;
        }
        public item Item
        {
            get ; set;
        }
        public int Capacity
        {
            get ; set;
        }
        public int NodeID
        {
            get ; set;
        }
        public int Weight
        {
            get ; set;
        }
        public int Category
        {
            get ; set;
        }
    }

And I tried to save it in my existing DB that has the following tables:

1) Category
2) Items
3) Nodes (Node and Items has 1-1 relationship)
4) Recipients
5) NodeRecipients (This table show the many to many relationship between Nodes and Recipients)

I use VS2012 to create an EF model as illustrated by the diagram below (please note that Nodes derives from Items in the EF)

enter image description here

I have a method that tries to save the nodes and its recipients

   public void SaveNodeAndRecipient(List<Nodes> MyNodes)
    {
        using (var db = new MyEntities())
        {
            foreach (var n in MyNodes)
            {
                Node n1 = new Node() { NodeID = n.NodeID, categoryID = n.Category, Capacity = n.Capacity };
                db.Items.Add(n1);
                foreach (var r in n.RecipientList)
                {
                    Recipient rep;
                    if (!db.Recipients.Select(x => x.recipientID).Contains(r))
                    {
                        rep = new Recipient() { recipientID = r };
                        db.Recipients.Add(rep);
                    }
                    else
                    {
                        rep = db.Recipients.Where(x => x.recipientID == r).FirstOrDefault();
                    }
                    Node_Recipient nr = new Node_Recipient(){RecipientID=r,NodeID=n.NodeID};
                    n1.Node_Recipient.Add(nr);
                }
            }
            db.SaveChanges();
        }
   }

MyEntities is the EF model and was declared in the appconfig:

  <connectionStrings>
    <add name="MyEntities" connectionString="xxxxx" providerName="System.Data.EntityClient" />
  </connectionStrings>

Everything was fine and compiled with no problem until I tried to savechanges. I got this error (not very descriptive)

enter image description here

Anyone knows what is going on? I am under the impression that the many to many relationship is the culprit but can not pin point what is causing it. Please help!

2 Answers
Related