问题描述
假设我有三个班。它是有效的实例化,但也有特殊情况B和D,它的子类A,增加额外的信息。
我会怎么做的映射文件这(流利)NHibernate的
大众A级
{
公众诠释ID {得到什么?;设置;}
公共字符串CommonProperty1 {搞定;组; }
公共字符串CommonProperty2 {搞定;组; }
}
公共B类:A
{
公共字符串BSpecificProperty1 {搞定;组; } // NOT NULL
公共字符串BSpecificProperty2 {搞定;组; } // NOT NULL
}
公共类D:A
{
公共字符串DSpecificProperty {搞定;组; } // NOT NULL
}
我试过以下,但它不工作在所有的:
公共类AMAP:ClassMap< A>
{
公共AMAP()
{
ID(X => x.ID);
地图(X => x.CommonProperty1);
地图(X => x.CommonProperty2);
}
}
公共类BMAP:ClassMap< B>
{
公共BMAP()
{
引用(X => x.ID);
地图(X => x.BSpecificProperty1)
.CanNotBeNull();
地图(X => x.BSpecificProperty2)
.CanNotBeNull();
}
}
公共类DMAP:ClassMap< D>
{
公共DMAP()
{
引用(X => x.ID);
地图(X => x.DSpecificProperty)
.CanNotBeNull();
}
}
我不敢肯定我理解你所说的映射子一到一的意思,但如果你想映射继承其中的子类有不可为空的属性,你可以流利,NHibernate的这样做:
//域类
公共类动物
{
公共虚拟INT标识{搞定;组; }
公共虚拟字符串名称{;组; }
}
公共类猫:动物
{
公共虚拟INT WhiskerLength {搞定;组; }
公共虚拟INT ClawCount {搞定;组; }
}
公共类犬:动物
{
公共虚拟INT TailWagRate {搞定;组; }
}
//映射文件
公共类AnimalMap:ClassMap<动物>
{
公共AnimalMap()
{
ID(X => x.Id)
.WithUnsavedValue(0)
.GeneratedBy.Native( );
地图(X => x.Name);
VAR catMap = JoinedSubClass< CAT和GT(CATID,SM => sm.Map(X => x.Id));
catMap.Map(X => x.WhiskerLength)
.CanNotBeNull();
catMap.Map(X => x.ClawCount)
.CanNotBeNull();
JoinedSubClass<狗>(DogId,SM => sm.Map(X => x.Id))
.MAP(X => x.TailWagRate)
.CanNotBeNull();
}
}
既然你想要的子类的属性是不可─空,你必须使用继承造型的表每个类(加入子类)的方式。这是因为表每个层次结构要求所有子类的属性为可以为空。
我希望它能帮助。
/埃里克
Suppose I have three classes. It is valid to instantiate A, but there are also special cases B and D which subclass A, adding additional information.
How would I do the mapping files for this in (fluent) NHibernate?
public class A
{
public int ID { get; set;}
public string CommonProperty1 { get; set; }
public string CommonProperty2 { get; set; }
}
public class B : A
{
public string BSpecificProperty1 { get; set; } //not null
public string BSpecificProperty2 { get; set; } //not null
}
public class D : A
{
public string DSpecificProperty { get; set; } //not null
}
I tried the following, but it doesn't work at all:
public class AMap : ClassMap<A>
{
public AMap()
{
Id(x => x.ID);
Map(x => x.CommonProperty1);
Map(x => x.CommonProperty2);
}
}
public class BMap : ClassMap<B>
{
public BMap()
{
References(x => x.ID);
Map(x => x.BSpecificProperty1)
.CanNotBeNull();
Map(x => x.BSpecificProperty2)
.CanNotBeNull();
}
}
public class DMap : ClassMap<D>
{
public DMap()
{
References(x => x.ID);
Map(x => x.DSpecificProperty)
.CanNotBeNull();
}
}
I'm not sure I understand what you mean by "map a subclass one-to-one", but if you want to map inheritance where the subclasses have properties that are not nullable, you can do like this in Fluent-NHibernate:
// Domain classes
public class Animal
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Cat : Animal
{
public virtual int WhiskerLength { get; set; }
public virtual int ClawCount { get; set; }
}
public class Dog : Animal
{
public virtual int TailWagRate { get; set; }
}
// Mapping file
public class AnimalMap : ClassMap<Animal>
{
public AnimalMap()
{
Id(x => x.Id)
.WithUnsavedValue(0)
.GeneratedBy.Native();
Map(x => x.Name);
var catMap = JoinedSubClass<Cat>("CatId", sm => sm.Map(x => x.Id));
catMap.Map(x => x.WhiskerLength)
.CanNotBeNull();
catMap.Map(x => x.ClawCount)
.CanNotBeNull();
JoinedSubClass<Dog>("DogId", sm => sm.Map(x => x.Id))
.Map(x => x.TailWagRate)
.CanNotBeNull();
}
}
Since you want the subclasses' properties to be not-null, you have to use the table-per-class (joined-subclass) way of modeling the inheritance. This is because table-per-hierarchy requires all subclass properties to be nullable.
I hope it helps.
/Erik
这篇关于功能NHibernate - 如何映射一个子类,一对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!