Fluent NHibernate:外键不为空问题

发布于 2024-08-05 12:31:03 字数 2050 浏览 6 评论 0原文

我有以下域类:

public class Installation : Entity<Installation>
{        
    public virtual string Name { get; set; }
    public virtual IList<Institution> Institutions { get; set; }

    public Installation()
    {
        Institutions = new List<Institution>();
    }
}
public class Institution : Entity
{
    public virtual string Name { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual Installation Installation { get; set; }        
}

我已根据以下内容创建了实体基类 帖子。我定义了以下映射:

public class InstitutionMapping : ClassMap<Institution> 
{
    public InstitutionMapping()
    {
        WithTable("Institution");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        Map(i => i.Address).Not.Nullable().WithLengthOf(50);
        Map(i => i.City).Not.Nullable().WithLengthOf(50);
        References(i => i.Installation).ColumnName("InstallationId").Not.Nullable().WithForeignKey();
    }
}
public class InstallationMapping : ClassMap<Installation>
{
    public InstallationMapping()
    {
        WithTable("Installation");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        HasMany<Institution>(i => i.Institutions).KeyColumnNames.Add("InstallationId").Cascade.All();
    }
}

当我运行以下代码时:

Installation installation = TestHelper.CreateAnonymousInstallation();
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(installation);

出现以下错误:

NHibernate.PropertyValueException: not-null 属性引用 null 或 瞬态值。

如何克服这个问题呢?

提前致谢 卢卡斯·格拉兹

I have the following domain classes:

public class Installation : Entity<Installation>
{        
    public virtual string Name { get; set; }
    public virtual IList<Institution> Institutions { get; set; }

    public Installation()
    {
        Institutions = new List<Institution>();
    }
}
public class Institution : Entity
{
    public virtual string Name { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual Installation Installation { get; set; }        
}

I have made the Entity base class according to the following post. I have the following mappings defined:

public class InstitutionMapping : ClassMap<Institution> 
{
    public InstitutionMapping()
    {
        WithTable("Institution");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        Map(i => i.Address).Not.Nullable().WithLengthOf(50);
        Map(i => i.City).Not.Nullable().WithLengthOf(50);
        References(i => i.Installation).ColumnName("InstallationId").Not.Nullable().WithForeignKey();
    }
}
public class InstallationMapping : ClassMap<Installation>
{
    public InstallationMapping()
    {
        WithTable("Installation");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        HasMany<Institution>(i => i.Institutions).KeyColumnNames.Add("InstallationId").Cascade.All();
    }
}

When I run the following code:

Installation installation = TestHelper.CreateAnonymousInstallation();
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(installation);

I get the following error:

NHibernate.PropertyValueException:
not-null property references a null or
transient value.

How to overcome the problem?

Thanks in advance
Lukasz Glaz

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

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

发布评论

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

评论(2

香橙ぽ 2024-08-12 12:31:03

我相信需要的是 inverse=true 。

HasMany<Institution>(i => i.Institutions)
   .KeyColumnNames.Add("InstallationId")
   .Cascade.All()
   .Inverse();

或者当然可以在安装类中添加一个方法来显式处理此问题。

public class Installation : Entity<Installation>
{        
    public virtual string Name { get; set; }
    public virtual IList<Institution> Institutions { get; set; }

    public Installation()
    {
        Institutions = new List<Institution>();
    }

    public virtual void AddInstitution(Institution entity)
    {
        entity.Installation = this;
        Institutions.Add(entity);
    }
}

I believe it's inverse=true that is needed.

HasMany<Institution>(i => i.Institutions)
   .KeyColumnNames.Add("InstallationId")
   .Cascade.All()
   .Inverse();

Or of course add a method in the installation class to handle this explicitly.

public class Installation : Entity<Installation>
{        
    public virtual string Name { get; set; }
    public virtual IList<Institution> Institutions { get; set; }

    public Installation()
    {
        Institutions = new List<Institution>();
    }

    public virtual void AddInstitution(Institution entity)
    {
        entity.Installation = this;
        Institutions.Add(entity);
    }
}
一人独醉 2024-08-12 12:31:03

正如我所看到的,您的机构课程有关于安装课程的参考。并且这个引用被设置为不为空。当您创建 AnonymousInstitution 时,您是否为其设置了一些安装?

As I can see your Institution class has reference on Installation class. And this reference is set to not null. When you are creating AnonymousInstitution do you set some Installation for it?

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