保存子对象时,流畅的NHibernate映射失败

本文关键字:NHibernate 映射 失败 对象 保存 | 更新日期: 2023-09-27 18:10:11

我确实尝试使用这个问题与Fluent Nhibernate建立一对多关系作为基础,但仍然存在问题。

我有两个类- Contact和PostalAddress - Contact可以有许多地址。问题是,当它使用PostalAddress到达类a时

会失败

Count=1的SqlParameterCollection索引1无效

有一个特定联系人的地址

Contact类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace YourLivesDataEngine.DataObjects
{
    [DataContract()]
    public class Contact
    {
        private String givenName;
        private String surname;
        private String middleName;
        public Contact()
        {
            Emails              = new List<EmailAddress>();
            Addresses           = new List<PostalAddress>();
            TelephoneNumbers    = new List<TelephoneNumber>();
            givenName           = String.Empty;
            surname             = String.Empty;
            middleName          = String.Empty;
            Id                  = Guid.NewGuid().ToString();
        }
        [DataMember]
        public virtual String Id { get; set; }
        [DataMember]
        public virtual DateTime LastUpdated { get; set; }
        [DataMember]
        public virtual String Network { get; set; }
        [DataMember]
        public virtual String NetworkId { get; set; }
        [DataMember]
        public virtual String Name { get; set; }
        [DataMember]
        public virtual String GivenName { get; set; }
        [DataMember]
        public virtual String Surname { get; set; }
        [DataMember]
        public virtual String MiddleName { get; set; }
        [DataMember]
        public virtual IList<EmailAddress> Emails { get; set; }
        [DataMember(Name="Addresses")]
        public virtual IList<PostalAddress> Addresses { get; set; }
        [DataMember]
        public virtual IList<TelephoneNumber> TelephoneNumbers { get; set; }

    }
}
<<p> PostalAddress类/strong>
public class PostalAddress
{
    public PostalAddress()
    {
        Id = Guid.NewGuid().ToString();
    }
    [DataMember]
    public virtual String Id { get; set; }
    public virtual String Address { get; set; }
    public virtual String Owner { get; set; }
    public virtual String AddressType { get; set; }
    public virtual Contact Contact { get; set; }
}

And my Mappings

ContactMap

public class ContactMap : ClassMap<Contact>
{
    public ContactMap()
    {
        Table("tblContacts");
        Id(x => x.Id);
        Map(x => x.LastUpdated);
        Map(x => x.Network);
        Map(x => x.NetworkId);
        Map(x => x.Name);
        Map(x => x.GivenName);
        Map(x => x.Surname);
        Map(x => x.MiddleName);
        HasMany<PostalAddress>(x => x.Addresses)
            .Inverse()
            .KeyColumns.Add("Id", mapping => mapping.Name("Id"))
            .Cascade.SaveUpdate()
    }
}

PostalAddressMap

   public class PostalAddressMap 
        : ClassMap<PostalAddress>
    {
        public PostalAddressMap()
        {
            Table("tblPostalAddresses");
            Id(x => x.Id);
            Id(x => x.Address);
            Id(x => x.AddressType);
            Id(x => x.Owner);
            References(x => x.Contact)
                .Class<Contact>()
                .Column("Owner");
        }
    }

保存联系人的代码为

   public void Save ( Contact Contact )
    {
        try
        {
            using (ISessionFactory factory = CreateSessionFactory())
            {
                using (var session = factory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        // save the contact
                        session.Save(Contact);
                        transaction.Commit();
                    }
                }
            }
        }
        catch ( System.Exception e)
        {
            throw e;
        }
        finally
        {
        }
    }

我确信这与映射设置的方式有关,但无法看到它,而有些联系人没有地址-有些人有,所以我应该至少有一些地址。

保存子对象时,流畅的NHibernate映射失败

在mxmissile的帮助下,现在已经修复了它。

问题都出在PostalAddress的映射上。首先,我设置了所有字段的Id,然后将所需的属性更改为Map。

然后得到无效索引4为这个SqlParameterCollection计数=4错误每次我试图保存地址。当一个字段被映射两次时,会导致此错误。对于这个映射,它是被映射两次的Owner。它不需要在Map中列出——通过注释它,应用程序就可以工作了。修改后的Mapping类如下:

public class PostalAddressMap 
    : ClassMap<PostalAddress>
{
    public PostalAddressMap()
    {
        Table("tblPostalAddresses");
        Id(x => x.Id);
        Map(x => x.Address);
        Map(x => x.AddressType);
        References(x => x.Contact)
            .Class<Contact>()
            .Column("Owner");
    }
}

修改完成后,应用程序正确地保存了联系人和地址。