流畅的Nhibernate Automapping with overrides:映射未映射的基类集合

时间:2015-12-14 18:20:38

标签: c# .net nhibernate fluent-nhibernate

域名:

public class BaseClassClient
{
    public virtual ICollection<BaseClass> BaseObjects{ get; set; }
}

public abstract class BaseClass
{
}

public class SubClass1 : BaseClass
{
}

public class SubClass2 : BaseClass
{
}

我得到错误Association references unmapped class,即使两个子类都已映射。显然,基类本身没有映射。请建议如何解决这个问题。

修改

我希望在将一个问题作为副本复制到另一个之前,他们只是不读,但也要关心这两个问题。

我已经看到了问题Error: fluent NHibernate mapping which references mapping in different assembly。但它谈到了一个不同的场景。

我的情况如下。

public class Product
{
    public int Id { get; set; }
    public ICollection<BasePricingRule> PricingRules{ get; set; }
}

public abstract class BasePricingRule
{
    public int Id { get; set; }
    //other properties
}

//Several concrete classes inherit from BasePricingRule.

我希望每个具体类都有一个表,并且没有基类的表。因此,BasePricingRule没有映射。我允许类自动映射,偶尔提供必要的覆盖。我为产品类创建了一个自动覆盖,如下所示。

    public void Override(FluentNHibernate.Automapping.AutoMapping<Product> mapping)
    {
        mapping.HasMany(product => product.PricingRules); //Not really sure
                                                          // about how to map this.
    } 

我一直在看http://ayende.com/blog/3941/nhibernate-mapping-inheritance这样的示例用于继承映射。但这些例子实际上并没有解决我所面临的问题。不仅仅是错误,我想知道如何通过流畅的NHibernate映射这个域,最好使用自动覆盖。

1 个答案:

答案 0 :(得分:0)

关键是,即使对于Table Per Concrete Class方法,您也必须映射基类。只是不是你想的方式。你应该看一下这篇好文章:www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

这或多或少会像这样:

public class BaseClassMap : ClassMap<BaseClass> {
    public BaseClassMap() {
        // indicates that this class is the base
        // one for the TPC inheritance strategy and that 
        // the values of its properties should
        // be united with the values of derived classes
        UseUnionSubclassForInheritanceMapping();
    }
}

public class SubClass1Map : SubclassMap<SubClass1> {
    public SubClass1Map() {
        Table("SubClass1Table");
        Abstract();
    }
}

public class SubClass2Map : SubclassMap<SubClass2> {
    public SubClass1Map() {
        Table("SubClass2Table");
        Abstract();
    }
}
相关问题