If you want to minimize the number of roundtrips to the database, here is a quick code snippet to remove an entity without having to fetch it first:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using(var context=new NorthwindContext()) | |
{ | |
var id=1; | |
var product= context.Products.Find(id); | |
context.Products.Remove(product); | |
context.SaveChanges(); | |
} |
Another option is to use a stub:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using(var context = new NorthwindContext()) | |
{ | |
var product = new Product { Id = 1 }; | |
context.Remove(product); | |
context.SaveChanges(); | |
} |