我有需要一对一关系的情况,因此使用HasOne
. 是的,我确实想为Patrons
and提供单独的表格Members
。以下是我的类及其对应的映射类。
public class Member
{
public virtual string ID { get; set; }
public virtual bool IsRegistered { get; set; }
public virtual Patron Patron { get; set; }
public virtual string Reference1 { get; set; }
public virtual string Reference2 { get; set; }
}
public class Patron
{
public virtual string ID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Address { get; set; }
public virtual int Telephone1 { get; set; }
public virtual int Telephone2 { get; set; }
public virtual int Age { get; set; }
public virtual string Occupation { get; set; }
public virtual string Gender { get; set; }
public virtual string Room { get; set; }
}
public class PatronMap : ClassMap<Patron>
{
public PatronMap()
{
Id(x => x.ID);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Gender);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Occupation);
Map(x => x.Telephone1);
Map(x => x.Telephone2);
Map(x => x.Room);
Table("Patrons");
}
}
public class MemberMap : ClassMap<Member>
{
public MemberMap()
{
Id(x => x.ID);
HasOne(x => x.Patron).PropertyRef(x => x.ID).Constrained();
Map(x => x.Reference1);
Map(x => x.Reference2);
Map(x => x.IsRegistered);
Table("Members");
}
}
我还有一个小测试,如下所示,看看这些东西是否正确映射。
[TestMethod]
public void MemberMap_Create_Success()
{
new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
.CheckProperty(x => x.ID, "1")
//I'm not quite sure about the following mapping check. How do I do HasOne check here? :-(
.CheckReference(x => x.Patron, new Patron()
{
ID = "2",
FirstName = "Foo",
LastName = "Bar",
Gender = "M",
Age = 59,
Address = "City, Coutnry",
Telephone1 = 0123456789,
Telephone2 = 0987654321,
Occupation = "Learning Fluent nHibernate",
Room = "Somewhere"
})
.CheckProperty(x => x.Reference1, "Ref1")
.CheckProperty(x => x.Reference2, "Ref2")
.CheckProperty(x => x.IsRegistered, true)
.VerifyTheMappings();
}
我不确定如何在此测试中测试 HasOne 映射属性。任何帮助表示赞赏。提前致谢。