【问题标题】:What are the disadvantages of keeping the parent as a property of the child in Fluent NHibernate?在 Fluent NHibernate 中将父母作为孩子的财产有什么缺点?
【发布时间】:2009-12-19 09:14:30
【问题描述】:

我正在使用 Fluent NHibernate/Automapping,并且我有一个包含集合的类,并且该集合中的每个项目都有自己的集合。

public class A
{
   public virtual IList<ClassB> Items { get; set; }
}

public class B
{
   public virtual IList<ClassC> ChildItems { get; set; }
}

这与this question 相关,该问题从未得到答复,但已由 OP 解决,将父对象保留在子对象上并将其标记为非空。

public class C
{
  [NotNull]
  public virtual ClassB Parent { get; set; }

  // Other stuff
}

这是在 Fluent NHibernate 中将外键设置为不为空的唯一方法吗?可能是一个愚蠢的问题,但我没有理由知道父对象是什么,所以拥有这些属性是没有用的。如果这是唯一的方法,这样做有什么缺点吗?如果我的代码可以只处理关系,那是否值得?

【问题讨论】:

    标签: c# fluent-nhibernate automapping


    【解决方案1】:

    假设您使用的是流畅映射而不是自动映射,您可以在映射文件中使用它:

    HasMany(x => x.ChildItems).Not.Null();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2021-08-08
      • 2020-06-26
      • 1970-01-01
      相关资源
      最近更新 更多