Hi friends,
I have been developing an application using nhibernate. I have my
repository and I need to create a SafeDelete method to avoid cascade
operations. The users want to see what entities has related data to check
when delete and avoid cascades. I'm trying to implement a generic solution
using criteria, but I do not know how to get the reference column of the
related type. I am using fluent nhibernate to map my entities. For sample:
public override void SafeDelete(T entity){
// get entity type
var type = entity.GetType();
// loop in properties
foreach (var propertyInfo in type.GetProperties())
{
// check if it is a generic type, such as collections:
List<Child> for sample
if (propertyInfo.PropertyType.IsGenericType)
{
// get the generic type
Type genericType =
propertyInfo.PropertyType.GetGenericArguments().FirstOrDefault();
// here, how to get the reference property ?
long count =
this.Session.CreateCriteria(genericType).Add(Restrictions.Eq("INVERSE
PROPERTY HERE", entity.Id))
.SetProjection(Projections.Count(Projections.Id()))
.UniqueResult<long>();
// check the count and throw an exception to parent layer
if (count > 0)
{
thrown new Exception("custom message");
}
}
}
// everything fine, delete!
base.Delete(entity);}
I have both types, deleting and related, how could I get the reference
property on the related type?
Thank you.
--
______________________________________
Felipe B Oriani
felipeoriani.com.br [email protected]
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.