Fluent Nhibernate: ignore interfaces/abstract classes during BuildSchema or UpdateSchema

允我心安 提交于 2019-12-12 03:47:10

问题


Edit

I've got an interface that I'd like to base some classes on. When I go to BuildSchema (or UpdateSchema), Nhibernate creates a table for the interface. I thought I found a work-around as follows:

The Interface and Classes:

public interface ILevel
{
    int Id { get; set; }
    string Name { get; set; }
}
public abstract class Level : ILevel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
public class LevelOne : Level { }
public class LevelTwo : Level { }

The Mappings:

public class LevelMap : ClassMap<ILevel>
{
    public LevelMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
    }
}
public class LevelOneMap : SubclassMap<LevelOne>
{
}
public class LevelTwoMap : SubclassMap<LevelTwo>
{
}

This doesn't work (I did another UpdateSchema and the pesky ILevel table appeared).

Is there a configuration I'm unaware of to ignore interfaces/abstract classes altogether?


回答1:


For seperate tables LevelOne and LevelTwo, that both have the properties of Level/ILevel, use a generic mapping for Level:

public abstract class LevelMap<T> : ClassMap<T>
    where T : Level  // ILevel should work, too
{
    protected LevelMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
    }
}

public class LevelOneMap : LevelMap<LevelOne>
{
}

public class LevelTwoMap : LevelMap<LevelTwo>
{
}

You provide two separate class mappings without telling Fluent the real class hierarchy.



来源:https://stackoverflow.com/questions/11693497/fluent-nhibernate-ignore-interfaces-abstract-classes-during-buildschema-or-upda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!