Fluent NThibnerate 域映射问题

发布于 2024-09-18 18:40:37 字数 1810 浏览 7 评论 0原文

我的域:

public class Person
{
    public Person() { }
    public virtual int PersonId { get; set; }
    public virtual string Title { get; set; }
    public virtual string FirstName { get; set; }
    public virtual IList<Address> Addresses { get; set; }
}
public class Address
{
    public Address()
    {}
    public virtual int AddressId { get; set; }
    public virtual Person AddressPerson { get; set; }
    public virtual string BuildingNumber { get; set; }
    public virtual string AddressLine1 { get; set; }
}

我的映射:

public class AddressMap : ClassMap<Address>
{
    public AddressMap()
    {
        Table("Address");
        LazyLoad();
        Id(x => x.AddressId).GeneratedBy.Identity();
        References(x => x.AddressPerson).Column("PersonId").Not.Nullable();
        Map(x => x.BuildingNumber).Length(250).Not.Nullable();
        Map(x => x.AddressLine1).Length(100).Not.Nullable();
    }
}

public class PersonMap : ClassMap<Person>
{
   public PersonMap()
   {
    Table("Person");
    LazyLoad();
    Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
    Map(x => x.Title).Length(6).Nullable();
    Map(x => x.FirstName).Length(100).Not.Nullable();
    HasMany(x => x.Addresses).KeyColumn("PersonId");
    HasMany(x => x.Applications).KeyColumn("PersonId");
   }
}

因此,当我尝试将地址添加到人员列表并保存时,出现以下错误:

对象引用未保存的瞬态 实例 - 保存瞬态实例 冲洗前。类型: Rise.Core.Domain.Address,实体: Rise.Core.Domain.Address

我是 NHibernate 的新手,我对到底发生了什么感到有点困惑。我相信我需要创建一个 BiDirectional 属性,或者我应该在保存人员 ID session.SaveOrUpdate(Person) 然后保存 session.SaveOrUpdate(Address) 后自己保存地址吗?不确定我到底做错了什么,我确实喜欢在 Person 上拥有可以延迟加载的地址列表,因为它使得编写一些 Linq 变得非常容易。

有什么建议吗?

My Domain:

public class Person
{
    public Person() { }
    public virtual int PersonId { get; set; }
    public virtual string Title { get; set; }
    public virtual string FirstName { get; set; }
    public virtual IList<Address> Addresses { get; set; }
}
public class Address
{
    public Address()
    {}
    public virtual int AddressId { get; set; }
    public virtual Person AddressPerson { get; set; }
    public virtual string BuildingNumber { get; set; }
    public virtual string AddressLine1 { get; set; }
}

My Mapping:

public class AddressMap : ClassMap<Address>
{
    public AddressMap()
    {
        Table("Address");
        LazyLoad();
        Id(x => x.AddressId).GeneratedBy.Identity();
        References(x => x.AddressPerson).Column("PersonId").Not.Nullable();
        Map(x => x.BuildingNumber).Length(250).Not.Nullable();
        Map(x => x.AddressLine1).Length(100).Not.Nullable();
    }
}

public class PersonMap : ClassMap<Person>
{
   public PersonMap()
   {
    Table("Person");
    LazyLoad();
    Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
    Map(x => x.Title).Length(6).Nullable();
    Map(x => x.FirstName).Length(100).Not.Nullable();
    HasMany(x => x.Addresses).KeyColumn("PersonId");
    HasMany(x => x.Applications).KeyColumn("PersonId");
   }
}

So when I attempt to add an address to the person list and save I get the following error:

object references an unsaved transient
instance - save the transient instance
before flushing. Type:
Rise.Core.Domain.Address, Entity:
Rise.Core.Domain.Address

I am new to NHibernate and I am a little confused as to what exactly is going on. I beleive I need to Create a BiDirectional attribute Or should I just be saving the address myself after I have saved the person ID session.SaveOrUpdate(Person) and then session.SaveOrUpdate(Address)? Not sure what exactly I am doing wrong, I do like having the list of address on Person that can be lazy loaded as it makes it really easy to write some Linq.

Any suggestions?

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

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

发布评论

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

评论(2

带刺的爱情 2024-09-25 18:40:37

我相信出现错误是因为您在保存地址所属的人之前尝试保存地址。

我认为你应该

1) 使用的空列表保存 Person P地址

2) 添加此 Person P 作为 A 的 AddressPerson 后保存地址 A

3) 将地址 A 添加到中P 人的地址

I believe the error came up since you tried to save an Address before saving the Person to whom it belonged.

I think you should

1) Save Person P with empty list of <IList> Addreses

2) Save Address A after adding this Person P as AddressPerson of A

3) Add Address A to <IList> Addresses of Person P

你穿错了嫁妆 2024-09-25 18:40:37

在 stackoverflow 上找到了一篇关于此的帖子!我只需要添加

.KeyColumn("PersonId")
.Inverse().Cascade.SaveUpdate();

Found a post on stackoverflow about this! I just need to add

.KeyColumn("PersonId")
.Inverse().Cascade.SaveUpdate();

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