/// <summary> /// Delete an existing instance from database. /// </summary> /// <param name="instance">Instance unique identifier.</param> private static int DeleteDatabaseForeignRecords(long id) { int rowsAffected = 0; object dummy; T instance = default; Type foreignType; MethodInfo deleteMethod; // Check if there are any foreign collection to delete in cascade foreach (ORMEntityMember member in ORMEntity <T> .ORMStructure.ForeignCollections) { // Get the owner instance if (instance == null) { instance = ORMEntity <T> .Get(id); } if (((ORMForeignCollection)member.Attribute).OnDeleteAction == OnDeleteActionTypes.DeleteInCascade) { // Get the method GET(long) foreignType = member.ForeignCollectionType; foreignType = typeof(ORMEntity <>).MakeGenericType(foreignType); deleteMethod = foreignType.GetMethod("Delete", new Type[] { typeof(Int64) }); // Delete each instance in collection foreach (long objId in ORMEntity <T> .GetForeignCollectionIdentifiers(instance, member)) { // Invoke the method dummy = Activator.CreateInstance(foreignType); rowsAffected += (int)deleteMethod.Invoke(dummy, new object[] { objId }); } } } return(rowsAffected); }