我有一组我想要批量删除的列表,但我通常必须首先附加对象是他们更干净的方式。我没有发现它必须为每个对象执行此操作我相信会有更简洁的方法来执行此操作吗?
public void DeleteCustomer(int CustomerId)
{
using (var myContext = new customerDbEntities())
{
Customer DeleteCustomer = myContext.Customers.Where(c => c.id == CustomerId).SingleOrDefault();
myContext.Customers.Remove(DeleteCustomer);
List<ContractDetail> DeleteContracts = myContext.ContractDetails.Where(c => c.CustomerId == CustomerId).ToList();
myContext.ContractDetails.RemoveRange(DeleteContracts);
List <Note> DeleteNotes = myContext.Notes.Where(c => c.CustomerId == CustomerId).ToList();
myContext.Notes.RemoveRange(DeleteNotes);
List <ProgamType> ProgTypes = myContext.ProgamTypes.Where(c => c.CustomerId == CustomerId).ToList();
myContext.ProgamTypes.RemoveRange(ProgTypes);
List<AddOn> AddonsList = myContext.AddOns.Where(c => c.CustomerId == CustomerId).ToList();
myContext.AddOns.RemoveRange(AddonsList);
List<revenue> revenueList = myContext.revenues.Where(c => c.customerid == CustomerId).ToList();
myContext.revenues.RemoveRange(revenueList);
List<Implentat> ImplentatList = myContext.Implentats.Where(c => c.customerId == CustomerId).ToList();
myContext.Implentats.RemoveRange(ImplentatList);
List<CustomerContact> CustomerContactsList = myContext.CustomerContacts.Where(c => c.CustomerId == CustomerId).ToList();
myContext.CustomerContacts.RemoveRange(CustomerContactsList);
List<CustomField> CustomFields = myContext.CustomFields.Where(c => c.CustomerId == CustomerId).ToList();
myContext.CustomFields.RemoveRange(CustomFields);
}
}
因为当我使用Attach时不允许列表作为类型实体,我应该传递什么,以及如何使这个更常见的例程。
List <Note> DeleteNotes = myContext.Notes.Where(c => c.CustomerId == CustomerId).ToList();
myContext.Notes.Attach(DeleteNotes); // Does not like here
myContext.Notes.RemoveRange(DeleteNotes);