c# - 具有覆盖 : Map an unmapped base class collection 的 Fluent Nhibernate Automapping

标签 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 映射此域,最好使用自动映射覆盖。

最佳答案

要点是,即使对于每个具体类的表方法,您也必须映射基类。只是不是你想的那样。您应该看看这篇很棒的文章: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();
    }
}

关于c# - 具有覆盖 : Map an unmapped base class collection 的 Fluent Nhibernate Automapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34273953/

相关文章:

c# - Gridview 上的 DataBind() 速度慢

c# - Thread.CurrentPrincipal.Identity 与 HttpContext.User.Identity

nhibernate - 使用 NHibernate 进行事件溯源

c# - 使用 Autofac 注册以 Service 结尾的所有内容

c# - 创建Pulumi.AzureNative.OperationalInsights.Workspace后如何获取WorkspaceKey?

接收 List<T> 的 C# 泛型方法不会为 T 的实际类型调用重载方法(更喜欢泛型)

c# - WPF 使用 MVVM 对集合进行分组

c# - XmlSerializer.Deserialize 是否将整个文档加载到内存中?

nhibernate - IPreUpdateEventListener 和动态更新 ="true"

c# - 将零宽度空间插入数据库的 Unicode 问题