Fluent nhibernate &引用;“无持久性”;在Fluent NHibernate中使用子类的流畅映射
我正在用流利的NHibernate做一件非常基本的事情。我发现很多人都有类似的问题,但似乎没有人能解决我的问题 我有一门课,比如:Fluent nhibernate &引用;“无持久性”;在Fluent NHibernate中使用子类的流畅映射,fluent-nhibernate,Fluent Nhibernate,我正在用流利的NHibernate做一件非常基本的事情。我发现很多人都有类似的问题,但似乎没有人能解决我的问题 我有一门课,比如: public abstract class ParentClass { public virtual long Id { get; private set; } public virtual DateTime CreateDate { get; set; } public virtual int Type { get; set; } } 和
public abstract class ParentClass
{
public virtual long Id { get; private set; }
public virtual DateTime CreateDate { get; set; }
public virtual int Type { get; set; }
}
和1类混凝土,如:
public class ChildClass : ParentClass
{
public virtual string PropertyX { get; set; }
public virtual int PropertyY{ get; set; }
}
所以我做了如下的映射:
public class ParentMap : ClassMap<ParentClass>
{
public ParentMap()
{
Id(x => x.Id);
Map(x => x.CreateDate);
DiscriminateSubClassesOnColumn("Type");
}
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard.ConnectionString(cstr => cstr.FromConnectionStringWithKey("TheConnectionString")))
.Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<ParentClass>()
.ExportTo(@"c:\temp\Mappings"))
.BuildSessionFactory();
}
但是,尽管该方法等待ParentClass变量,我还是为它传递了一个ChildClass实例(该方法实际上是接口的继承,这就是它需要ParentClass的原因)
每次我看到它在“SaveOrUpdate”方法上都会出现一个错误,说“没有用于:ChildClass的持久化器”
我做错了什么
另一件奇怪的事情是,即使在SessionFactory创建中使用“ExportTo”方法,也没有在文件夹上写入映射。更改
.Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<ParentClass>()
.Mappings(Mappings=>Mappings.FluentMappings.AddFromAssemblyOf())
到
.Mappings(Mappings=>Mappings.FluentMappings.AddFromAssemblyOf())
更改
.Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<ParentClass>()
.Mappings(Mappings=>Mappings.FluentMappings.AddFromAssemblyOf())
到
.Mappings(Mappings=>Mappings.FluentMappings.AddFromAssemblyOf())
Ahhh..链接到类而不是映射。非常微妙,但非常耗时。感谢mxmissile,+1。Ahhh..链接到类而不是映射。非常微妙,但非常耗时。感谢mxmissile,+1。
.Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<ParentClass>()
.Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<ParentMap>()