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 B B{get;set;}
}
public class C2 : BaseClass2
{
public int Id {get;set;}
public virtual B B {get;set;}
}
...
public class C100 : BaseClass100
{
public int Id {get;set;}
public virtual B B {get;set;}
}
class A has collection of class B and class Ci have one class B and different base classes. when in class A collection there is only B that Ci not reference it, I can delete class A and all the B collection also deleted(cascade delete). But when in class A collection there is B that classes Ci has reference to it I can't delete class A instance...
My expected behavior:
Class A will be deleted and all the B collection that class A has, and if Ci has reference to some of the B in the collection it will be null in the end of the delete.( class Ci intances will not be deleted!), also I don't want to iterate throw all my Ci class to see if it has reference to the B collection that need to be deleted.
I don't want 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;
}
}
someone know how to achieve it?