流利Nhibernate:在BuildSchema或UpdateSchema期间忽略接口/抽象类 [英] Fluent Nhibernate: ignore interfaces/abstract classes during BuildSchema or UpdateSchema
问题描述
我有一个接口,我想建立一些类。当我转到BuildSchema(或UpdateSchema)时,Nhibernate为接口创建一个表。我想我找到了一个解决方法如下:
接口和类:
public interface ILevel
{
int Id {get;组; }
string Name {get;组; }
public abstract class Level:ILevel
{
public virtual int Id {get;组; }
public virtual string Name {get;组; }
}
public class LevelOne:Level {}
public class LevelTwo:Level {}
映射:
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>
{
}
这不起作用(我做了另一个UpdateSchema,出现了讨厌的ILevel表)。
有没有一个配置我不知道忽略接口/抽象类呢?
对于单独的表LevelOne和LevelTwo,它们都具有Level / ILevel的属性,使用Level的一个通用映射: b
$ b
public abstract class LevelMap< T> :ClassMap< T>
其中T:Level // ILevel也应该工作
{
protected LevelMap()
{
Id(x => x.Id);
Map(x => x.Name);
}
}
公共类LevelOneMap:LevelMap< LevelOne>
{
}
公共类LevelTwoMap:LevelMap< LevelTwo>
你提供了两个单独的类映射,类层次结构。
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?
解决方案 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.
这篇关于流利Nhibernate:在BuildSchema或UpdateSchema期间忽略接口/抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!