简体   繁体   中英

Fluent NHibernate auto mapping 2 class to one entity

How to auto mapping two class to one database entity?

public abstract class Root
{
        public virtual int Id { get; set; }
        public virtual string Item { get; set;}
}

public class First
{
        public override string ToString()
        {
                return "First " + Id;
        }
}

public class Second
{
        public override string ToString()
        {
                return "Second " + Id;
        }
}

This code generate 2 entity to database.

The Fluent NHibernate documentation explains it well. The first step is to have your extended classes inherit from the base class:

public class First : Root

Since your base class is abstract you instruct FNH to not map it as a concrete class:

AutoMap.AssemblyOf<Entity>(cfg).IgnoreBase<Entity>();

FNH defaults to using a table-per-subclass mapping strategy. If you want to use table-per-hierarchy mapping (which I recommend), override the IsDiscriminated method:

public override bool IsDiscriminated(Type type)
{
   return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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