Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除时显示相关实体_C#_Hibernate_Nhibernate_Cascade - Fatal编程技术网

C# 删除时显示相关实体

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

我有一个与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 { 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方法。