【问题标题】:Fluent NHibernate is bringing ClassMap Id and SubClassMap Id to referenced table?Fluent NHibernate 将 ClassMap Id 和 SubClassMap Id 带入引用表?
【发布时间】:2011-02-22 12:04:21
【问题描述】:

嗨,

我正在尝试映射以下实体:

public class Product {
   public int ProductId { get; private set; }
   public string Name { get; set; }
}

public class SpecialProduct : Product {
   public ICollection<Option> Options { get; private set; }
}

public class Option {
   public int OptionId { get; private set; }
}

还有以下映射:

public class ProductMap : ClassMap<Product> {
  public ProductMap() {
    Id( x => x.ProductId );
    Map( x => x.Name );
}

public class SpecialProductMap : SubclassMap<SpecialProduct> {
  public SpecialProductMap() {
    Extends<ProductMap>();
    HasMany( p => p.Options ).AsSet().Cascade.SaveUpdate();
  }
}

public class OptionMap : ClassMap<Option> {
  public OptionMap() {
    Id( x => x.OptionId );
  }
}

问题是我的表格最终是这样的:

Product
--------
ProductId
Name

SpecialProduct
--------------
ProductId

Option
------------
OptionId
ProductId          // This is wrong
SpecialProductId   // This is wrong

应该只有一个 ProductId 和一个对 SpecialProduct 表的引用,但我们得到“两个”Id 和两个对 SpecialProduct.ProductId 的引用。

有什么想法吗?

谢谢 安迪

【问题讨论】:

  • 你能展示一下你希望你的桌子如何结束吗?
  • 有两个ProductId 列,因为SpecialProduct 表必须以某种方式连接回Product,它是一个外键。至于Option 表,看起来确实是错误的。您可能遇到了错误。我已经为它开了一个ticket,我稍后会调查它。
  • 我无法使用提供的信息重现您的问题。请您评论我之前链接到的票,并提供尽可能多的额外信息。重现该问题的精简解决方案将非常好。

标签: c# nhibernate fluent-nhibernate mapping


【解决方案1】:

感谢大家的反馈。

我想要的表格如下所示:

Product 
-------- 
ProductId 
Name 

SpecialProduct 
-------------- 
ProductId 

Option 
------------ 
OptionId 
SpecialProductId   // Which ends up being just product id, but the FK here is to SpecialtyProduct

我忘记在我原来的问题中将这一行添加到 OptionMap 类中:

public class OptionMap : ClassMap<Option> {      
  public OptionMap() {      
    Id( x => x.OptionId );      
    References( x => x.ParentOption );
  }      
} 

如果我然后改用它,它会按我的意愿工作:

public class OptionMap : ClassMap<Option> {      
  public OptionMap() {      
    Id( x => x.OptionId );      
    References( x => x.ParentOption ).Column( "SpecialProductId" ).Not.Nullable();
  }      
} 

由于引用,Fluent 似乎正在添加“ProductId”,并且没有发现相应的列已经存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2011-01-05
    • 2010-10-12
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多