流畅的NHibernate PersistentSpecification测试失败

时间:2013-11-27 16:23:44

标签: c# nhibernate fluent-nhibernate

我目前有这些地图

public class CountryMap : ClassMap<Country>
{
    public CountryMap()
    {
        Table("tblCountry");
        Id(x => x.Id, "intCountryId");
        Map(x => x.Name, "strCountryName");
        HasMany(x => x.FlagImages)
            .Table("tblImage")
            .KeyColumn("intRelId")
            .Where("intObjId=29")
            .Not.Cascade.All();
    }
}

public class ImageMap : ClassMap<Image>
{
    public ImageMap()
    {
        Table("tblImage");
        Id(x => x.Id, "intImgId");
        Map(x => x.ObjId, "intObjId");
        Map(x => x.RelId, "intRelId");
        Map(x => x.ImageName, "strImage");
    }
}

以及以下持久性规范测试:

        PersistenceSpecification<Image>(session)()
            .CheckProperty(r => r.Id, 1)
            .CheckProperty(r => r.ImageName, "ss")
            .CheckProperty(r => r.ObjId, (int)ObjectType.Country)
            .CheckProperty(r => r.RelId, 102)
            .VerifyTheMappings();

Image的测试失败了。

失败消息是 Assert.AreEqual failed. Expected:<(null)>. Actual:<NHibernate.Exceptions.GenericADOException: could not insert: [HansaCrew.Models.Images.Image][SQL: INSERT INTO tblImage (intObjId, intRelId, strImage) VALUES (?, ?, ?); select last_insert_rowid()] ---> System.Data.SQLite.SQLiteException: constraint failed foreign key constraint failed

我已经检查了2天但没找到原因。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。

这段代码的问题在于

        HasMany(x => x.FlagImages)
        .Table("tblImage")
        .KeyColumn("intRelId")
        .Where("intObjId=29")
        .Not.Cascade.All();

NHibernate会自动使用tblImage创建一个外键约束,该约束有很多intRelId s,它不在表intCountryId的{​​{1}}列中。这就是测试无法说出外键约束失败的原因。

解决此问题的方法是在数据库中创建tblCountry,然后将view映射到view模型