how to achieve table per concrete class when base class is abstract in fluent nhibernate?

后端 未结 2 1080
慢半拍i
慢半拍i 2021-02-08 11:00

i have the following scenario

public abstract class BaseClass
{
  public virtual int Id {get; set};
  public virtual string Name {get; set;}
}

public class Fir         


        
2条回答
  •  孤独总比滥情好
    2021-02-08 11:41

    Since I haven't seen your mapping, let me provide mine. You could achieve this by doing like this

    public class BaseClassMap:ClassMap
    {
        public BaseClassMap()
        {
            /*
             * Identity generator can't be native because subclass objects should be unique
             * So use HiLo or Guid or other generators which will generate unique id on the child tables
             */
            Id(x => x.Id).GeneratedBy.Guid(); 
            Map(x => x.Name);
            UseUnionSubclassForInheritanceMapping(); // This is important - uses union-subclass mappings for the derived classes
        }
    }
    
    public class FirstSubClassMap : SubclassMap
    {
        public FirstSubClassMap()
        {
            Table("FirstSubClassTable");
            // Map properties for FirstSubClass
        }
    }
    
    public class SecondSubClassMap : SubclassMap
    {
        public SecondSubClassMap()
        {
            Table("SecondSubClassTable");
            // Map properties for SecondSubClass
        }
    }
    

提交回复
热议问题