1

有一个ResourcePackage类和一个PackageItem类:

public ResourcePackageMap()
{
    DiscriminatorValue((int)ResourceObjectType.Package);

    HasMany(x => x.Children).KeyColumn("AggregationObjectId").Cascade.AllDeleteOrphan();
}

public PackageItemMap()
{
    Id(x => x.Id, "AggregationLinkId");
    References(x => x.ResourceItem, "ChildObjectId");
    References(x => x.Package, "AggregationObjectId");
    Map(x => x.Order, "OrderWithinAggregation");
    Map(x => x.Usage, "Usage");

    Table("tbl_Object_Aggregation_Link");
}

我收到一条错误消息:

无法删除集合:[Domain.ResourcePackage.Children#102c589b-fc1c-451d-8300-a0ef00baa21f][SQL:
更新 tbl_Object_Aggregation_Link SET AggregationObjectId = null WHERE AggregationObjectId = @p0]

NHibernate.Exceptions.GenericADOException:无法删除集合:
[域.ResourcePackage.Children#102c589b-fc1c-451d-8300-a0ef00baa21f]
[SQL:更新 tbl_Object_Aggregation_Link SET AggregationObjectId = null WHERE
AggregationObjectId = @p0] ---> System.Data.SqlClient.SqlException:
无法将值 NULL 插入“AggregationObjectId”列,
表'KDatabase.dbo.tbl_Object_Aggregation_Link';
列不允许空值。
更新失败。该语句已终止。

关系表运行如下:

有一个 tbl_Object 表和一个 tbl_Object_Aggregation_Link 表,其中包含 tbl_Object 表的两个外键。

而 tbl_Object_Aggregation_Link 表的映射类为:

public class PackageItemMap : ClassMap<PackageItem>
{
    public PackageItemMap()
    {
        Id(x => x.Id, "AggregationLinkId");
        References(x => x.ResourceItem, "ChildObjectId");
        References(x => x.Package, "AggregationObjectId");
        Map(x => x.Order, "OrderWithinAggregation");
        Map(x => x.Usage, "Usage");

        Table("tbl_Object_Aggregation_Link");
    }
}
4

1 回答 1

0

要么改变

       HasMany(x => x.Children).KeyColumn("AggregationObjectId").Cascade.AllDeleteOrphan()

       HasMany(x => x.Children).KeyColumn("AggregationObjectId").Cascade.All()

或者使'tbl_Object_Aggregation_Link'表中的两个外键都接受空值。

编辑(另一种解决方案):

判断地图:

References(x => x.Package, "AggregationObjectId");

我猜你要么将属性(包)作为 null 传递,并且数据库不允许此外键为 null,要么数据库中不存在此包的 ID。所以要么让列"AggregationObjectId"接受 null,要么传递一个有效的包对象(你可以使用 Nhibernate 获取它并传递它),或者像这样制作地图:

 References(x => x.Package, "AggregationObjectId").Cascade.All();

但是如果在数据库中找不到它,这将自动保存包。

于 2012-10-22T05:08:15.237 回答