none
Updating and Deleting Many-to-Many relationship in EF 4.1 RRS feed

  • Question

  • Hello everyone,

    I'm working with EF 4.1 (DbContext) and trying to update Many-to-Many relationships but in vain. Here are the details:

    Topic: TopicId, TopicDetails
    Tag: TagId, TagName
    TagTopic: TopicId, TagId

    [HttpPost]
            public ActionResult Edit(Topic topic, string[] selectedTags)
            {
                ModelState.Remove("Tags");
                if (ModelState.IsValid)
                {
                    for (int i = 0; i < selectedTags.Count(); i++)
                    {
                        var tag = db.Tags.Find(Convert.ToByte(selectedTags[i]));
                        topic.Tags.Add(tag); // I'm expecting that EF 4.1 would automatically add the missing tags and delete not selected tags; BUT seems like this line has NO effect
                    }

                    db.Entry(topic).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.Tags = db.Tags;
                return View(topic);
            }

     

    I'm expecting that EF 4.1 would automatically add the missing tags and delete not selected tags; but seems like this has not effect. Kindly help me with this.

    Thanks

     

    Tuesday, August 23, 2011 11:59 PM

Answers

  • Hi sas79gi,

    Welcome!

    The Delete code here:

     [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
          try
          {
            // TODO: Add update logic here
            var topic = context.Topics.Find(id);
            context.Entry(topic).Collection(t => t.Tags).Load();
            topic.Tags.Remove(topic.Tags.First());
            context.SaveChanges();
            return RedirectToAction("Index");
          }
          catch
          {
            return View();
          }
        }
    

    The Remove method just delete the TopicTag Table's records and the actually records won't be deleted.
    The Update code here:

    If you just want to update the properties, you just to retrieve them out and change the properties then SaveChanges:

    try
          {
            // TODO: Add update logic here
            var topic = context.Topics.Find(id);
            context.Entry(topic).Collection(t => t.Tags).Load();
            topic.Tags.First().Content = "t";
            context.SaveChanges();
            return RedirectToAction("Index");
          }
          catch
          {
            return View();
          }
    

    If you want update relationship, I think you can remove and then add:

     [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
          try
          {
            // TODO: Add update logic here
            var topic = context.Topics.Find(id);
            context.Entry(topic).Collection(t => t.Tags).Load();
            topic.Tags.Remove(topic.Tags.First());
            topic.Tags.Add(new Tag { Content = "hh" });
            context.SaveChanges();
            return RedirectToAction("Index");
          }
          catch
          {
            return View();
          }
        }
    

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by sas79gi Friday, August 26, 2011 1:41 AM
    Wednesday, August 24, 2011 12:29 PM
    Moderator