Fluent NHibernate Mapping设置为AllDeleteOrphan,但仍尝试将DB中的外键置空

时间:2011-02-01 15:01:08

标签: nhibernate fluent-nhibernate nhibernate-mapping

当我尝试在具有一对多关系的任何表上执行ISession.Delete时,我收到了NHibernate的错误。

NHibernate试图将子表中父表的外键设置为null,而不是仅删除子表行。

这是我的域名:

public class Parent
{

    public Parent()
    {
        _children = new List<Child>();
    }

    public int Id { get; set; }
    public string SimpleString { get; set; }
    public DateTime? SimpleDateTime { get; set; }

    private IList<Child> _children;
    public IEnumerable<Child> Children
    {
        get { return _children; }
    }
    public void AddChild(Child child)
    {
        child.Parent = this;
        _children.Add(child);
    }
}


public class Child
{
    public int Id { get; set; }
    public string SimpleString { get; set; }
    public DateTime? SimpleDateTime { get; set; }
    [JsonIgnore]
    public Parent Parent { get; set; }
}

我已按如下方式设置了Fluent NHibernate映射:

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        Not.LazyLoad();
        Id(x => x.Id);
        Map(x => x.SimpleString);
        Map(x => x.SimpleDateTime);

        HasMany(x => x.Children)
            .Not.LazyLoad()
            .KeyColumn("ParentId").Cascade.AllDeleteOrphan()
            .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore);

    }
}

public class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        Not.LazyLoad();
        Id(x => x.Id);
        Map(x => x.SimpleString);
        Map(x => x.SimpleDateTime);
        References(x => x.Parent).Not.Nullable().Column("ParentId").Cascade.All().Fetch.Join();
    }
}

我已经告诉NHibernate Cascade.AllDeleteOrphan()但它仍然试图将ParentId foriegn键设置为null这是我设置的测试:

public void Delete_GivenTableWithChildren_WillBeDeletedFromDB()
{
    int id;
    using (var createSession = MsSqlSessionProvider.SessionFactory.OpenSession())
    {
        var parent = new Parent();
        parent.AddChild(new Child { SimpleString = "new child from UI" });

        using (var trx = createSession.BeginTransaction())
        {
            createSession.Save(parent);
            trx.Commit();
            id = parent.Id;
        }
    }

    using (var firstGetSession = MsSqlSessionProvider.SessionFactory.OpenSession())
    {
        var result = firstGetSession.Get<Parent>(id);
        Assert.IsNotNull(result);
    }

    using (var deleteSession = MsSqlSessionProvider.SessionFactory.OpenSession())
    {
        using (var trx = deleteSession.BeginTransaction())
        {
            deleteSession.Delete("from " + typeof(Parent).Name + " o where o.Id = :Id", id, NHibernateUtil.Int32);
            trx.Commit();
        }
    }

    using (var session = MsSqlSessionProvider.SessionFactory.OpenSession())
    {
        var result = session.Get<Parent>(id);
        Assert.IsNull(result);
    }
}

尝试以下SQL后,deleteSession.Delete行失败了:

exec sp_executesql N'UPDATE [Child] SET ParentId = null WHERE ParentId = @p0',N'@p0 int',@p0=5

使用:

NHibernate.Exceptions.GenericADOException : could not delete collection: [SaveUpdateOrCopyTesting.Parent.Children#5][SQL: UPDATE [Child] SET ParentId = null WHERE ParentId = @p0]
  ----> System.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'ParentId', table 'SaveUpdateCopyTestingDB.dbo.Child'; column does not allow nulls. UPDATE fails.
The statement has been terminated.

有没有人知道我在映射中做错了什么,或者知道如何阻止NHibernate尝试将外键id归零?

由于

戴夫

2 个答案:

答案 0 :(得分:19)

尝试在ParentMap的HasMany上设置.Inverse(),如下所示:

HasMany(x => x.Children)
        .Not.LazyLoad()
        .KeyColumn("ParentId").Cascade.AllDeleteOrphan().Inverse()
        .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore);

答案 1 :(得分:0)

如果您使用HQL删除,我不确定cascade是否有效。

试试这个:

var parent = deleteSession.Load<Parent>(id)
deleteSession.Delete(parent);

很遗憾你没有延迟加载。 Load不需要从数据库中读取实体,它只会在内存中创建代理。