【问题标题】:Fluent Nhibernate Automapping. An association refers to an unmapped base class流畅的休眠自动映射。关联是指未映射的基类
【发布时间】:2013-05-20 09:47:50
【问题描述】:

我有以下情况:

带有 db 实体的

AssemblyX 引用外部 assemblyY 并将其类用作基类。 AssemblyX 添加了一些导航必要时的属性。

当然我可以复制所有这些实体并使用 automapper 来解决我的问题,但我有理由继承现有实体。

所以,我在 assemblyY 中有 Foo {} 类,并在 assemblyX 中创建 Bar: Foo {}。配置流利的自动映射以忽略Foo (.IgnoreBase<Foo>())。一切正常。但是 assemblyY 有另一个实体 Rab(由流式自动映射覆盖),它具有引用 Foo 的导航属性。这会导致异常:“来自表 Rab 的关联引用了未映射的类:Foo”。

public class Foo // .IgnoreBase<Foo>() - I require only derived Bar in DB
{
    public virtual Guid Id {get; set;}        
}

public class Bar: Foo
{
    public virtual Guid Id {get; set;}
    // Some new properties
}

public class Rab
{
    public virtual Guid Id {get; set;}
    public virtual Foo Foo // reference to unmapped class, I could not change type to Bar (external assembly)
}

我该如何解决。我尝试继承 Rab 并创建 Bar 类型的 new Foo 属性,但没有成功。

提前感谢您的任何建议。

【问题讨论】:

    标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    您可以使用自定义 IAutomappingConfiguration 来确定应该映射哪个类型或成员:

    class MyConfig : DefaultAutomappingConfiguration
            {
                public override bool ShouldMap(Type type)
                {
                    return  !(typeof(Foo)).IsAssignableFrom(type);
                }
    
                public override bool ShouldMap(Member member)
                {
                    return  !(typeof(Foo)).IsAssignableFrom(member.PropertyType);                }
            }
    

    然后你可以像这样使用你的配置:

    AutoPersistenceModel model = new AutoPersistenceModel(new MyConfig());
    

    最后:

    Fluently.Configure()
    
                        .Mappings(m => m.UsePersistenceModel(model))
                        .BuildSessionFactory();
    

    【讨论】:

      猜你喜欢
      • 2011-11-15
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多