开发者

Fluent NHibernate is bringing ClassMap Id and SubClassMap Id to referenced table?

HI,

I have the following entities I'm trying to map:

public class Product {
   public int ProductId { get; private set; }
   public string Name { get; set; }
}开发者_Python百科

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

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

And the following mappings:

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 );
  }
}

The problem is that my tables end up like this:

Product
--------
ProductId
Name

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

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

There should only be the one ProductId and single reference to the SpecialProduct table, but we get "both" Ids and two references to SpecialProduct.ProductId.

Any ideas?

Thanks Andy


Thanks for the feedback everyone.

The tables I wanted look like this:

Product 
-------- 
ProductId 
Name 

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

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

I had forgotten to add this line in in my original question to the OptionMap class:

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

If I then use this instead, it works as I want:

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

It looks Fluent was adding "ProductId" due to the references and didn't figure out that the appropariate column was already there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜