保存子对象时 Fluent NHibernate 映射失败

Fluent NHibernate mapping failing when saving child objects

我确实尝试使用这个问题 Setting up one to many relationship with Fluent Nhibernate 作为基础,但仍然有问题。

我有两个 classes - Contact 和 PostalAddress - 一个 Contact 可以有多个地址。问题是当它到达带有 PostalAddress 的 class a 时它失败并显示

Invalid index 1 for this SqlParameterCollection with Count=1.

该特定联系人有一个地址

联系Class

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; }


    }
}

邮政地址Class

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; }
}

还有我的映射

联系人地图

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
        {

        }

    }

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

在 mxmissile 的帮助下现已修复。

问题都在 PostalAddress 的映射中。首先,我制作了所有字段的 ID - 因此将所需的属性更改为 Map。

每次我尝试保存地址时,都会收到此 SqlParameterCollection 的索引 4 无效且 Count=4 错误。当一个字段被映射两次时会导致此错误。对于此映射,Owner 被映射了两次。它不需要在地图中列出 - 通过将其注释掉,应用程序就可以工作。修改后的Mappingclass如下;

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");

    }
}

完成这些更改后,应用程序会正确保存联系人和地址。