C# 删除时显示相关实体
我有一个与NHibernate映射了一些关系的模型,它运行良好,例如:C# 删除时显示相关实体,c#,hibernate,nhibernate,cascade,C#,Hibernate,Nhibernate,Cascade,我有一个与NHibernate映射了一些关系的模型,它运行良好,例如: public class A { public int Id { get; set; } // other properties public ICollection<B> BList { get; set; } public ICollection<C> CList { get; set; } public ICollection<D> DList { g
public class A
{
public int Id { get; set; }
// other properties
public ICollection<B> BList { get; set; }
public ICollection<C> CList { get; set; }
public ICollection<D> DList { get; set; }
}
You cannot delete this register because there are relations with:
-B
-D
(如果一个a
实体具有B
或D
的关系,而不是C
)
我知道我可以一个实体一个实体地检查信息,但我想有一个通用的解决方案,有什么办法吗 NHibernate有自己的元数据API,允许您读取所有映射信息,包括哪些集合映射到属性,以及属性类型。 从每个属性的类型中,可以找到相关类型的名称
A instance = ...
var metaData = this.session.SessionFactory.GetClassMetadata(instance.GetType());
foreach(IType propertyType in metaData.PropertyTypes)
{
if(propertyType.IsCollectionType)
{
var name = propertyType.Name;
var collectionType = (NHibernate.Type.CollectionType)propertyType;
var collection = collectionType.GetElementsCollection(instance);
bool hasAny = collection.Count > 0;
}
}
您的映射解决方案是什么?HBML,Fluent…?我正在使用Fluent nhibernate…。好的,但是我怎么能检查这些收集的数据?!更新了我的答案-您可以通过提供实例来提取实际收集。感谢您的awnser Alexander,但是,您知道是否有任何方法可以从Cascade例外中提取此信息吗?!在哪里可以找到
collectionType.GetElementsCollection(实例)代码>命名空间?请参阅位于的nhibernate文档,请参阅CollectionType。它具有GetElementsCollection方法。