更新时间:2023-02-13 16:26:31
执行此操作的***方法是使用 stub实体:仅具有ID值的实体对象:
The best way to do this is by using a stub entity: an entity object that's only got an Id value:
var product = context.Products.First();
var photo = new ProductPhoto { ProductId = product.ProductId }; // Stub
context.Entry(photo).State = System.Data.Entity.EntityState.Deleted;
context.Products.Remove(product);
context.SaveChanges();
如果您知道产品
的ID您甚至可以仅创建两个存根就可以删除 Product
及其 ProductPhoto
。
If you know a Product
's Id you can even delete both the Product
and its ProductPhoto
by only creating two stubs.