我确实尝试使用此问题Setting up one to many relationship with Fluent Nhibernate作为基础,但仍有问题。
我有两个类 - Contact和PostalAddress - 一个联系人可以有很多地址。问题是当它到达具有PostalAddress的类a时,它失败了
此SqlParameterCollection的索引1无效且Count = 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; }
}
}
邮政地址类
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
{
}
}
我确定这与设置映射的方式有关但却无法看到它,而有些联系人没有地址 - 有些联系人应该至少有一些地址。< / p>
答案 0 :(得分:0)
在mxmissile的帮助下,现在已经修复了它。
在PostalAddress的映射中存在问题。首先,我创建了所有字段ID - 所以将所需的属性更改为Map。
每当我尝试保存地址时,为此SqlParameterCollection获取了无效索引4,其中Count = 4错误。当字段映射两次时会导致此错误。对于这个映射,它是两次映射的所有者。它不需要在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");
}
}
完成这些更改后,应用程序正确保存了“联系人和地址”。