简体   繁体   中英

Delete related object in Linq to SQL by updating parent object

When I want to add a new related objects I can use this method to do so:

  • pull the parent object from the database,
  • find the property (it's an EntitySet I believe) holding the one-to-many relation to the other model,
  • insert a new child object there
  • and finally save the parent class

Example:

var post = db.Post.Single(x => x.Id == 1);
post.Tag.Add(new Tag(){ name="Brand new!" };
db.SubmitChanges();

This may look like some extra hassle in compare to simply adding the child object directly to the DB, but when one has to use generic services (meaning access to DBContext is being obscured inside of a controller) to handle DB operations it can be actually convenient (well sometimes).

Now, is it possible to delete a related object in a similar fashion?

I remember experimenting with the idea before, but never succeeding (I think the exceptions were mentioning the fact that object being attached to the DB as a reason for failures).

I know I can simply write:

var model = db.Tag.Single(x => x.PostId == 1 && name == "Brand new!");
db.Tag.DeleteOnSubmit(model);
db.SubmitChanges();

and be done with it. In the context of my project such code would be placed in an custom service inheriting from the generic one. The point is, I just want to know if a related object can be deleted while updating the parent object or not. If it's possible, that might help me produce some prototype code faster, if not I just handle it the standard way.

var model = db.Tag.Single(x => x.PostId == 1 && name == "Brand new!");
db.Tag.DeleteObject(model);
db.SubmitChanges();

Is what you need.

Or you can this way either :

db.Tag.Where(x => x.PostId == i && name == "Brand New!").ToList().ForEach(Tag.Tag.DeleteObject); db.SaveChanges();

Or This way should also work:

db.Tag.Delete(x => x.PostId == 1 && name == "Brand new!");

Maybe my post will help someone :)

Assuming you have a nullalbe foreign key between Post and Tag, and association is made by Tag.PostID == Post.ID

I make it this way: in your code you just remove a child item from collection (right as you wished):

var post = db.Post.Single(x => x.Id == 1);
Tag tagToRemove = post.Tag.First();//select a tag to remove
post.Tag.Remove(tagToRemove);

//Somewhere you call submit
db.SubmitChanges();

After this (if your foreign key is nullable) you will have a tag in you DB with PostID == null. To avoid this, you can extend you DataContext class with one partial method:

public partial class YourDataContext
{        
    partial void UpdateTag(Tag instance)
    {
        //If PostID is null
        if(!instance.PostID.HasValue)
        {
            //Delete this record from the DB
            this.ExecuteDynamicDelete(instance);
        }
    }
}

Instead of last piece of code some advise to set "DeleteOnNull" property in .dbml file (inaccessible from designer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM