我嘗試過使用這個問題Setting up one to many relationship with Fluent Nhibernate作爲基礎,但仍然存在問題。保存子對象時流暢的NHibernate映射失敗
我有兩個類 - 聯繫人和PostalAddress - 聯繫人可以有很多地址。問題是當它到達一類具有的PostalAddress它失敗
無效索引1這SqlParameterCollection以計數= 1。
有針對特定聯繫人地址
聯繫類
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; }
}
}
的PostalAddress類
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; }
}
我的映射
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
{
}
}
我敢肯定,這是值得做的方式映射設置但ca沒有看到它,雖然有一些聯繫人沒有地址 - 有些人這樣做,我應該至少有一些地址。
您是否通過PersistenceSpecification測試人員驗證了您的地圖?不要在我附近有VS,所以語法可能會關閉:https://gist.github.com/mxmissile/d81682def1d88a522767 – mxmissile
不 - 我剛剛通過在PostalAddressMap中提及所有者的註釋掉了問題中的錯誤。現在它不會將Id寫入PostalAddress記錄,因此如果將Id設置爲null(由於它是主鍵而不允許),因此不允許爲null。 –
首先,您的PostalAddressMap是錯誤的,您將屬性映射爲Id。 – mxmissile