I have this scenario: I have these classes:
public class A { public int Id {get;set;} public virtual ICollection<B> bCollection {get; set; } } public class B { public int Id {get;set;} } public class C1 : BaseClass1 { public int Id{get;set;} public virtual BB{get;set;} } public class C2 : BaseClass2 { public int Id {get;set;} public virtual BB {get;set;} } ... public class C100 : BaseClass100 { public int Id {get;set;} public virtual BB {get;set;} }
class A has a set of classes B, while class Ci has one class B and different base classes . when there is only B in the collection of class A that Ci does not reference it, I can delete class A and also delete all collections of B (cascading deletion). But , when there is B in the collection of class A that the Ci classes have a reference to it, I cannot delete an instance of class A ...
Expected Behavior:
Class A will be deleted, and the entire collection B that class A has, and if Ci refers to some of B in the collection, it will be null at the end of the deletion. (Ci classes will not be deleted!), also I do not want to iterate over my entire Ci class to see if there is a link to assembly B that needs to be removed.
I do not need this code:
MyDbContext db=new MyDbContext(); Hash<int> bCollectionToDelete=GetBCollectionToDeleteHashSet(); var C1_db_collection= db.C1Collection.ToList(); foreach(var c1Item in C1Collection) { if(bCollectionToDelete.Contains(c1Item.refrenceIdToB) { c1Item.refrenceIdToB=null; } } var C2_db_collection= db.C2Collection.ToList(); foreach(var c2Item in C1Collection) { if(bCollectionToDelete.Contains(c2Item.refrenceIdToB) { c2Item.refrenceIdToB=null; } } ... var C100_db_collection= db.C100Collection.ToList(); foreach(var c100Item in C100Collection) { if(bCollectionToDelete.Contains(c100Item.refrenceIdToB) { c100Item.refrenceIdToB=null; } }
Does anyone know how to achieve it?
source share