开发者

How to do inheritance in fluent NH without discriminator type column

I have 2 classes:

public class MyBaseClass
{
    public virtual int Id  { get; set; }
    public virtual string BaseProperty { get; set; }
}

public class MyClass : MyBaseClass
{
    public virtual string ChildProperty { get; set; }
}

I want to map each of them to its own table (fluent NH). How to do it with no discriminator type column added to [MyBaseClass] table? So I expect [MyBaseClass] table consists of BaseProperty and Id columns only, MyClass consists of Id, BasePro开发者_Go百科perty and ChildProperty columns.

Thanks


You can try to put IgnoreBase to on MyBaseClass. It will say for FNH to map those classes independently


I've just found this (http://wiki.fluentnhibernate.org/Fluent_mapping#Components):

public class Parent
{
  public int Id { get; set; }
  public string Name { get; set; }
}

public class Child : Parent
{
  public string AnotherProperty { get; set; }
}

If you wanted to map this as a table-per-subclass, you'd do it like this:

public class ParentMap : ClassMap<Parent>
{
  public ParentMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
  }
}

public class ChildMap : SubclassMap<Child>
{
  public ChildMap()
  {
    Map(x => x.AnotherProperty);
  }
}

So looks like this approach does not require any Db changes like adding special fields to my tables. The only problem is I do not know how to do the same in AutoMapping with Override statements. We do mapping this way:

public class AutoMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool IsDiscriminated(Type type)
    {
        return true;
    }

    public override bool ShouldMap(Type type)
    {
        return type.In(typeof(MyBaseClass),typeof(MyClass),...)
    }
...
}

FluentNHibernate.Automapping.AutoPersistenceModel Instance =
            AutoMap.AssemblyOf<MyBaseClass>(new AutoMappingConfiguration())             
    .Override<MyBaseClass>(m =>
{
        ...
    }
}

So I'm not sure how to apply SubClass instruction in my case. Any advise?

Thanks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜