I'm trying to check if my entity has any dependencies before allowing deletion:
Normally we can do this for each entity type by doing this:
public class Company{
public int id;
public string name;
public virtual IList<Department> Departments { get; set; }
public virtual IList<Building> Buildings {get;set;}
public bool hasDependencies => Departments.Any() || Buildings.Any();
}
But I want to do this generically for any nagivational properties an entity could have
Note: Lazy loading is enabled.
public bool HasDependencies()
{
int ExampleEntityId = 2; // just example
Company companyEntity = _unitofwork.CompanyRepository.GetEntityById(ExampleEntityId);
IEnumerable<PropertyInfo> propertyInfoList = _unitofwork.CompanyRepository.GetNavigationProperties();
bool dependenciesExist = false;
foreach (PropertyInfo property in propertyInfoList)
{
dependenciesExist = companyEntity.**property**.Any(); //This obviously doesn't work, but just to convey the intent.
if (dependenciesExist) {
break;
};
}
return dependenciesExist;
}
This is the GetNavigationProperties method:
public IEnumerable<PropertyInfo> GetNavigationProperties()
{
var modelData = _context.Model.GetEntityTypes(typeof(TEntity)).ToList()[0];
var propertyInfoData = modelData.GetNavigations().Select(x => x.PropertyInfo);
return propertyInfoData;
}
I've looked in many pages including the following to figure this out:
EF5 How to get list of navigation properties for a domain object
Check if entity has references in other entities in Code First
Aucun commentaire:
Enregistrer un commentaire