Fluent NHibernate插入父id问题与HasMany关系

发布于 2024-12-04 21:06:05 字数 1050 浏览 0 评论 0原文

我不明白为什么 NHibernate 会在没有父实体外键的情况下插入子实体。 我发现解决这个问题的唯一方法是使用双向关系,还有其他方法吗?

以下是这些类:

public class Parent
{
    public virtual int ParentId {get; private set;}
    public virtual IList<Child> Notes {get; private set;}
}

public class Child
{
    public virtual ChildId {get; private set;}
    public virtual Name {get; private set;}
}

这是我的 Fluent NHibernate 映射

public class ParentClassMap : ClassMap<Parent> 
{
    public ParentClassMap(){
        Id(x => x.ParentId);
        HasMany(x => x.Notes).Cascade.AllDeleteOrphan();
    }
}

public class ChildClassMap : ClassMap<Child> 
{
    public ChildClassMap() {
        Id(x => x.ChildId);
        Map(x => x.Name);
    }
}

当我将一个子级添加到父级的子级集合并保存父级时,父级和子级将被插入到数据库中,但子级的插入没有父级的外键(它有一个空值)

这是生成的插入:

INSERT INTO Child(ChildId, Name)

但它应该是:

INSERT INTO Child(ChildId, Name, ParentId)

我想补充一点,我不想用双向关系来解决这个问题,我不希望子级拥有对父级的引用。谢谢!!

I can't understand why NHibernate is inserting a child entity without the foreign key of the parent.
The only way I found to solve this is with a Bidirectional relationship, is there another way?

Here are the classes:

public class Parent
{
    public virtual int ParentId {get; private set;}
    public virtual IList<Child> Notes {get; private set;}
}

public class Child
{
    public virtual ChildId {get; private set;}
    public virtual Name {get; private set;}
}

Here is my Fluent NHibernate mapping

public class ParentClassMap : ClassMap<Parent> 
{
    public ParentClassMap(){
        Id(x => x.ParentId);
        HasMany(x => x.Notes).Cascade.AllDeleteOrphan();
    }
}

public class ChildClassMap : ClassMap<Child> 
{
    public ChildClassMap() {
        Id(x => x.ChildId);
        Map(x => x.Name);
    }
}

When I add a child to the parent's child collection and save the parent, the parent and the child are inserted into the database, but the child is inserte withoutthe foreign key to the parent (it has a null value)

This is the insert that is generated:

INSERT INTO Child(ChildId, Name)

But it should be:

INSERT INTO Child(ChildId, Name, ParentId)

I want to add that i don't want to resolve this with a bidirectional relationship, i do not want the child to have a reference to the parent. Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2024-12-11 21:06:05

Not.KeyNullable() 添加到您的 HasMany 映射中。

注意:此功能需要 NHibernate 3.2。

Add Not.KeyNullable() to your HasMany mapping.

Note: This feature requires NHibernate 3.2.

鸵鸟症 2024-12-11 21:06:05

您的父类映射应该有一个其子类的逆映射,

public class ParentClassMap : ClassMap<Parent> 
{
    public ParentClassMap(){
        Id(x => x.ParentId);
        HasMany(x => x.Notes).Cascade.AllDeleteOrphan().Inverse();
    }
}

谢谢
尼勒什

Your parent class mapping should have an inverse for its child

public class ParentClassMap : ClassMap<Parent> 
{
    public ParentClassMap(){
        Id(x => x.ParentId);
        HasMany(x => x.Notes).Cascade.AllDeleteOrphan().Inverse();
    }
}

Thanks
Neelesh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文