0

几天来,我一直在为这个烂摊子苦苦挣扎,试图找出错误的确切原因。一个字,失败!基本上,我一直在尝试对ClassMap我写的reference一个引用另一个reference的 .

    [TestMethod]
    public void IssueReturnRegisterMap_Create_Success()
    {
        var maxDifference = TimeSpan.FromMilliseconds(990);
        BookSize sz = new BookSize() { ID = "1", Name = "A" };
        Department dpt = new Department() { ID = "1", Name = "Philosophy" };
        Author auth = new Author() { ID = "2", FirstName = "Wise", LastName = "Person" };
        Publisher pub = new Publisher() { ID = "1", Name = "Pub1", Address = "Address 1" };
        Language lang = new Language() { ID = "1", Name = "Lang1" };
        Patron ptrn = new Patron()
        {
            ID = "1",
            FirstName = "First",
            LastName = "Last",
            Age = 82,
            Address = "Address1",
            Gender = "Male",
            Occupation = "Occupation1",
            Telephone1 = "01234567890",
            Telephone2 = "01234567890"
        };

        Book book = new Book()
        {
            ID = "1",
            Name = "Book1",
            Number = "12",
            BookSize = sz,
            Department = dpt,
            Author = auth,
            Commentator = "Another wiseman",
            Publisher = pub,
            Language = lang,
        };
        Member member = new Member()
        {
            ID = "1",
            Patron = ptrn,
            IsRegistered = true
        };

        //Edit: Acc. to Firo's suggestion
        Database.Session.Save( sz );
        Database.Session.Save( dpt );
        Database.Session.Save( auth );
        Database.Session.Save( lang );
        Database.Session.Save( pub );
        Database.Session.Save( ptrn );
        Database.Session.Save( book ); //@Firo: Throws same exception here! :-(
        Database.Session.Save( member );

        new PersistenceSpecification<IssueReturnEntry>( Database.Session, new CustomEqualityComparer() )
        .CheckProperty( x => x.ID, "1" )
        .CheckReference( x=> x.Book, book )
        .CheckReference( x => x.Member, member )
        .CheckProperty( x => x.IssuedBy, "Librarian" )
        .CheckProperty( x => x.IssueDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) )
        .CheckProperty( x => x.DueDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) )
        .CheckProperty( x => x.ReturnDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) )
        .CheckProperty( x => x.ReturnBy, "User" )
        .VerifyTheMappings();
    }

你可以清楚地看到 myBook是由referenceslikeBookSize等组成的Department。然后它Book本身就变成了一个referencesforIssueReturnRegister地图。对于Member. 它为对象引发“非空属性引用空值或瞬态值错误”异常BookSize。任何帮助是极大的赞赏。提前致谢。

4

3 回答 3

0

该错误意味着 Booksize 被要存储的实体引用,但它本身没有存储,并且不能存储在此操作中,因为禁用了级联。

所以要么做

  • References(x => x.BookSize).Cascade.All()在映射或
  • Database.Session.Save(bz)跑步前PersistenceSpecification

所有其他引用的实体都相同

更新:持久实体应该有一个 Equals 方法,将 id 考虑在内

public override bool Equals(object obj)
{
    var other = obj as BookSize;
    return other != null && (Id == 0) ? ReferenceEquals(this, other) : Id == other.Id;
}

private int? _hashcode;
public override int GetHashCode()
{
    return _hashcode.HasValue ? _hashcode.Value : _hashcode = Id;
}
于 2013-07-26T15:40:04.347 回答
0

我的BookMapandBookSizeMap如下所示,然后遵循Book实体的单元测试,使用PersistenceSpecification它通过没有任何此类异常(请注意,我已经尝试按照Book实体的单元测试进行操作,但不适用于IssueRegisterEntry实体。

public class BookMap : ClassMap<Book>
{
    public BookMap()
    {
        Id(x => x.ID);
        Map(x => x.Name);
        References(x => x.BookSize)
        .Column("SizeID");
        References(x => x.Department)
        .Column("DepartmentID");
        Map(x => x.Number);
        Map(x => x.Edition);
        Map(x => x.CopyNumber);
        References(x => x.Author)
        .Column("AuthorID");
        Map(x => x.Commentator);
        References(x => x.Publisher)
        .Column("PublisherID");
        References(x => x.Language)
        .Column("LanguageID");
        Map(x => x.Cupboard);
        Map(x => x.IsIssued);
        Map(x => x.IsDeleted);
        Table("Books");
    }
}

public class BookSizeMap : ClassMap<BookSize>
{
    public BookSizeMap()
    {
        Id(x => x.ID);
        Map(x => x.Name);
        Map(x => x.Length);
        Map(x => x.Width);
    }
}

    [TestMethod]
    public void BookMapping_Create_Success()
    {
        new PersistenceSpecification<Book>( Database.Session, new CustomEqualityComparer() )
        .CheckProperty(x => x.ID, "1")
        .CheckProperty(x => x.Name, "My Experiments with Truth")
        .CheckReference(x => x.BookSize, new BookSize() { ID = "2", Name = "A" })
        .CheckReference(x => x.Department, new Department() { ID = "3", Name = "Autobiography" })
        .CheckProperty(x => x.Number, "ABG.a.1")
        .CheckProperty(x => x.Edition, "12")
        .CheckProperty(x => x.CopyNumber, "13")
        .CheckReference(x => x.Author, new Author() { ID = "4", FirstName = "Mohandas", LastName = "Gandhi" })
        .CheckProperty(x => x.Commentator, "Comm1")
        .CheckReference(x => x.Publisher, new Publisher() { ID = "5", Name = "ABC", Address = "XYZ" })
        .CheckReference(x => x.Language, new Language() { ID = "6", Name = "Lang1" })
        .CheckProperty(x => x.IsIssued, true)
        .CheckProperty(x => x.IsDeleted, false)
        .VerifyTheMappings();
    }
于 2013-08-13T14:53:37.063 回答
0

抱歉,Firo,我认为我的回复不够可读,因此再次在这里发布。

if (x is BookSize && y is BookSize)
{ 
return (((BookSize)x).ID == ((BookSize)y).ID) && (((BookSize)x).Name == ((BookSize)y).Name) && (((BookSize)x).Length == ((BookSize)y).Length) && (((BookSize)x).Width == ((BookSize)y).Width);
}

这是我为 EqualityComparer 获得的 equals 方法。我还没有任何 GetHashCode 方法。

于 2013-07-31T20:09:10.927 回答