Fluent NHibernate 有很多子表没有主键的映射

发布于 2024-09-12 05:11:31 字数 2482 浏览 4 评论 0原文

我已经使用 NHibernate 几周了,所以请耐心等待。我正在开发一个存在很多奇怪问题的遗留数据库。

我有一个名称对象

public class Name    {    
        public Name()
        {
            Addresses = new List<Address>();
        }    
        public virtual string Id { get; set; }
        public virtual string FirstName { get; set; }    
        public virtual string LastName { get; set; }     
        public virtual IList<NameAddress> Addresses { get; set; }    
}

它有地址的子对象

public class Address 
{
        public virtual string NameId { get; set; }
        public virtual string Line1 { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string Zipcode { get; set; }
        public virtual string Type { get; set; }
        public virtual bool IsPreferrred { get; set; }
}

这是映射 Address 没有定义主键,但 name_id 和 type 使其唯一。您看到的列是表结构。

   Table("ADDRESS");
    CompositeId()
            .KeyProperty(x => x.NameId, "NAME_ID")
            .KeyProperty(x => x.Type, "ADDRESS_TYPE");
    Map(x => x.IsPreferred)
        .Column("PREF");
    Map(x => x.Line1)
        .Column("ADDRESS1");
    Map(x => x.Line2)
        .Column("ADDRESS2");
    Map(x => x.City)
        .Column("CITY");
    Map(x => x.State)
        .Column("STATE");

姓名 表(“名称”);

        Id(x => x.Id)
            .GeneratedBy.Custom<XXIdentifierGenerator>(p => p.AddParam("prefix", "NAME"))
            .Column("NAME_ID");
        Map(x => x.Prefix)               
            .Column("NAME_PRE");
        Map(x => x.FirstName)
            .Column("NAME_FIRST");
        HasMany(x => x.Addresses)
            .KeyColumn("NAME_ID")
            .Table("ADDRESS")
            .LazyLoad();

我可以创建一个没有任何地址的名称并获取生成的 id。

 repository.Save(name); // only calls session.Save and does a commit 
 Address address = new NameAddress {IsPreferred = true, Type= "Home", Line1 = "123 Main St",
                                    City = "Anytown", State = "CT", Zipcode="06512" };
 name.Addresses.Add(address);
 repository.SaveOrUpdate(name);

当我尝试保存地址时,出现异常

     {"Unexpected row count: 0; expected: 1"}

我不确定

  1. 我的映射是否错误
  2. 我不知道如何连接有很多
  3. 我无法在没有主键的情况下执行此操作
  4. 在这种情况下地址是否必须自己能得救吗?

谢谢, 保罗

So I'm a couple of weeks into NHibernate so pleae bear with me. I am working on a legacy database with lots of weird issues.

I have a name object

public class Name    {    
        public Name()
        {
            Addresses = new List<Address>();
        }    
        public virtual string Id { get; set; }
        public virtual string FirstName { get; set; }    
        public virtual string LastName { get; set; }     
        public virtual IList<NameAddress> Addresses { get; set; }    
}

It has children of address

public class Address 
{
        public virtual string NameId { get; set; }
        public virtual string Line1 { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string Zipcode { get; set; }
        public virtual string Type { get; set; }
        public virtual bool IsPreferrred { get; set; }
}

Here are the mappings
Address has no primary key defined but it is name_id and type that make it unique. The columns you see are the table structure.

   Table("ADDRESS");
    CompositeId()
            .KeyProperty(x => x.NameId, "NAME_ID")
            .KeyProperty(x => x.Type, "ADDRESS_TYPE");
    Map(x => x.IsPreferred)
        .Column("PREF");
    Map(x => x.Line1)
        .Column("ADDRESS1");
    Map(x => x.Line2)
        .Column("ADDRESS2");
    Map(x => x.City)
        .Column("CITY");
    Map(x => x.State)
        .Column("STATE");

Name
Table("NAME");

        Id(x => x.Id)
            .GeneratedBy.Custom<XXIdentifierGenerator>(p => p.AddParam("prefix", "NAME"))
            .Column("NAME_ID");
        Map(x => x.Prefix)               
            .Column("NAME_PRE");
        Map(x => x.FirstName)
            .Column("NAME_FIRST");
        HasMany(x => x.Addresses)
            .KeyColumn("NAME_ID")
            .Table("ADDRESS")
            .LazyLoad();

I can create a name without any addresss and get the generated id back.

 repository.Save(name); // only calls session.Save and does a commit 
 Address address = new NameAddress {IsPreferred = true, Type= "Home", Line1 = "123 Main St",
                                    City = "Anytown", State = "CT", Zipcode="06512" };
 name.Addresses.Add(address);
 repository.SaveOrUpdate(name);

When I try to save the address I get an exception

     {"Unexpected row count: 0; expected: 1"}

I am not sure if

  1. My mapping is wrong
  2. I don't get how to wire up a has many
  3. I can't do this without a primary key
  4. In this case do the addresses have to be saved on their own?

Thanks,
Paul

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

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

发布评论

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

评论(1

不语却知心 2024-09-19 05:11:31

这有点盲目,但是如何在 HasMany(x => x.Addresses) 映射上添加 .Cascade.All 或类似的 代码>名称?

我想 NHibernate 需要知道您想要将新地址与新名称一起插入。

This is a bit of a shot in the dark, but how about adding .Cascade.All or similar on the HasMany(x => x.Addresses) mapping for Name?

I would imagine that NHibernate needs to know that you want new Addresses to be inserted along with a new Name.

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