简体   繁体   English

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

[英]Fluent Nhibernate Automapping with overrides: Map an unmapped base class collection

The domain: 域名:

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

public abstract class BaseClass
{
}

public class SubClass1 : BaseClass
{
}

public class SubClass2 : BaseClass
{
}

I get the error Association references unmapped class , even though both the sub classes are mapped. 我得到错误Association references unmapped class ,即使两个子类都被映射。 Obviously, the base class itself is not mapped. 显然,基类本身没有映射。 Please suggest how to solve this. 请建议如何解决这个问题。

Edit 编辑

I would rather that before closing a question as a duplicate to another, they just not read, but also care to understand both the questions. 在关闭一个问题作为另一个问题之前,我宁愿这样做,他们只是不读,但也关心理解这两个问题。

Ihave seen the question Error: fluent NHibernate mapping which references mapping in different assembly . 我已经看到了错误:流畅的NHibernate映射,它引用了不同程序集中的映射 But it talks about a different scenario. 但它谈到了一个不同的场景。

My scenario is like below. 我的情况如下。

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.

I want to have one table per concrete class and no table for the base class. 我想每个具体类有一个表,基类没有表。 Therefore, there is no mapping for the BasePricingRule . 因此, BasePricingRule没有映射。 I am allowing the classes to get auto mapped, occassionally providing necessary overrides. 我允许类自动映射,偶尔提供必要的覆盖。 I created an automapping override for the product class as below. 我为产品类创建了一个自动覆盖,如下所示。

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

I have been seeing examples like http://ayende.com/blog/3941/nhibernate-mapping-inheritance for inheritance mappings. 我已经看到像http://ayende.com/blog/3941/nhibernate-mapping-inheritance这样的示例用于继承映射。 But these examples actually not address the issue I am facing. 但这些例子实际上并没有解决我所面临的问题。 More than the error, I would like to know how I should map this domain via fluent NHibernate, preferably using automapping overrides. 不仅仅是错误,我想知道我应该如何通过流畅的NHibernate映射这个域,最好是使用自动覆盖。

The point is that, even for the Table Per Concrete Class approach, you will have to map the base class. 关键是,即使对于Table Per Concrete Class方法,您也必须映射基类。 Just not the way you may be thinking. 只是不是你想的方式。 You should take a look at this great article: www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat 你应该看一下这篇很棒的文章: www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

It would be more or less like this: 它或多或少会像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM