0

我有需要一对一关系的情况,因此使用HasOne. 是的,我确实想为Patronsand提供单独的表格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 映射属性。任何帮助表示赞赏。提前致谢。

4

2 回答 2

1

以下是我为实现HasOne映射和单元测试所做的工作。地图如下:

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.AshramRoom);
        HasOne(x => x.Member).ForeignKey();
        Table("Patrons");
    }
}

public class MemberMap : ClassMap<Member>
{
    public MemberMap()
    {
        Id(x => x.ID);
        Map(x => x.IsRegistered);
        Map(x => x.Reference1);
        Map(x => x.Reference2);
        References(x => x.Patron)
            .Column("PatronID")
            .Unique()
            .UniqueKey("IDX_UniquePatronID");
        Table("Members");
    }
}

单元测试是这样的:

public void MemberMap_Create_Success()
{
    new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
    .CheckProperty(x => x.ID, "1")
    .CheckReference(x => x.Patron, new Patron()
    {
        ID = "2",
        FirstName = "Abc",
        LastName = "Xyz",
        Gender = "M",
        Age = 99,
        Address = "Address",
        Telephone1 = 0000000001,
        Telephone2 = 1000000000,
        Occupation = "Occupation",
        AshramRoom = "Room"
    })
    .CheckProperty(x => x.Reference1, "Ref1")
    .CheckProperty(x => x.Reference2, "Ref2")
    .CheckProperty(x => x.IsRegistered, true)
    .VerifyTheMappings();
}

希望这可以帮助某人。:-)

于 2013-05-29T21:26:49.357 回答
0

我知道已经有一段时间了,但如果有帮助的话。

您可以使用:

var patrol = 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"
    };

...

    .CheckReference(x => x.Patron, patrol)
    .CheckProperty(x => x.Reference1, patrol)
    .CheckProperty(x => x.Reference2, patrol)

请参阅:https ://github.com/jagregory/fluent-nhibernate/wiki/Persistence-specification-testing

于 2013-05-18T14:04:14.827 回答