I have an application with a database that doesn't allow cascading deletes. When a user goes to delete an entity, I would like to display a popup saying something generic like, This record has dependencies that must be deleted first.To support this, I was hoping to write some code that could generically take an entity, check for virtual properties, and if they exist, iterate through them to see if any records exist. If it finds one, it would immediately return true.The goal would be to return a true/false with as little lazy loading as possible.Before I write the code to do this, just wanted to see if anyone has done this before. I guess I could just look for the exception, but I generally don't like coding for exceptions.
Scan Relationships of Entity Framework Entity Generically
asp.net;entity framework;generics
This is pretty rough, but it accomplishes what I was trying to do:internal static bool HasChildren(object obj){    //Get Properties    var props = obj.GetType().GetProperties();    foreach (var property in props)    {        //Look for Navigation Properties        if (property.GetGetMethod().IsVirtual)        {            var value = property.GetValue(obj);            //Typically instantiated, but just in case, look for null            if (value != null)            {                //Since most navigation propertie are HastSet<T>, it's going to be tough trying to                //reflect on the object using a type. Going to look for a count prop instead.                if (property.PropertyType.GetProperty(Count) != null)                {                    var count = property.PropertyType.GetProperty(Count).GetValue(value);                    if (Convert.ToInt16(count) > 0)                    {                        return true;                    }                }            }        }    }    return false;}