我有以下课程
public class Contact
{
public Contact() {
Addresses = new List<Address>();
EmailAddresses = new List<EmailAddress>();
PhoneNumbers = new List<PhoneNumber>();
}
public virtual int ContactID { get; private set; }
public virtual Firm Firm { get; set; }
public virtual ContactType ContactType { get; set; }
public virtual string FullName { get; set; }
public virtual string FiscalCode { get; set; }
public virtual string Notes { get; set; }
public virtual ContactRole ContactRole { get; set; }
public virtual IList<Address> Addresses { get; private set; }
public virtual IList<EmailAddress> EmailAddresses { get; private set; }
public virtual IList<PhoneNumber> PhoneNumbers { get; private set; }
}
public class Address
{
public virtual int AddressID { get; private set; }
public virtual string StreetAddress { get; set; }
public virtual string ZipCode { get; set; }
public virtual string City { get; set; }
public virtual Province Province { get; set; }
public virtual Country Country { get; set; }
public virtual Contact Contact { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual bool PostalAddress { get; set; }
}
这些类已使用FluentNHibernate映射到数据库。这些是映射类
public ContactMap() {
Table( "Contacts" );
Id( c => c.ContactID ).Column( "ContactID" ).GeneratedBy.Identity();
References( c => c.Firm ).Column( "FirmID" );
References( c => c.ContactType ).Column( "ContactTypeID" );
Map( c => c.FullName );
Map( c => c.FiscalCode );
Map( c => c.Notes );
References( c => c.ContactRole ).Column( "ContactRoleID" );
HasMany( c => c.Addresses ).Cascade.SaveUpdate();
HasMany( c => c.EmailAddresses ).Cascade.SaveUpdate();
HasMany( c => c.PhoneNumbers ).Cascade.SaveUpdate();
}
public AddressMap() {
Table( "Addresses" );
Id( a => a.AddressID ).Column( "AddressID" ).GeneratedBy.Identity();
Map( a => a.StreetAddress );
Map( a => a.ZipCode );
Map( a => a.City );
References( a => a.Province ).Column( "ProvinceID" );
References( a => a.Country ).Column( "CountryID" );
References( a => a.Contact ).Column( "ContactID" );
References( a => a.AddressType ).Column( "AddressTypeID" );
Map( a => a.PostalAddress );
}
我正在尝试使用这些类在数据库中加载大量的联系人。创建这些对象的代码可以在逻辑上解释如下
Create a contact
For each address of this contact
create an address
set the contact address
add the address to the contact collection
Next
For each email of this contact
create an email
set the contact email
add the email to the contact collection
Next
我对所有后代集合都没有问题,比如Email,PhoneNumber,除了我的地址有问题。事实上,当我尝试插入至少有一个地址的联系人时,我收到以下错误
Could not insert collection: [GSLConverter.Entities.Contact.Addresses#3551][SQL: UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1]
Invalid column name 'AuthorID'
使用AuthorID代替使用ContactID。 AuthorID来自哪里????
这些是NHibernate在服务器上执行的查询
INSERT INTO Addresses (StreetAddress, ZipCode, City, PostalAddress, ProvinceID, CountryID, ContactID, AddressTypeID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
select SCOPE_IDENTITY();
@p0 = 'xxx xxxxxx, 69 ', @p1 = '80142', @p2 = 'xxxxxx', @p3 = False, @p4 = 1, @p5 = 113, @p6 = 3632, @p7 = 1
UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1;
@p0 = 3632, @p1 = 26
@Stefan Steinegger:这是引用AuthorID成员的类(唯一一个)的映射
public IssueNoteMap() {
Table( "IssueNotes" );
Id( ino => ino.IssueNoteID ).Column( "IssueNoteID" ).GeneratedBy.Identity();
References( ino => ino.Issue ).Column( "IssueID" );
Map( ino => ino.NoteDate );
Map( ino => ino.NoteTitle );
Map( ino => ino.NoteBody );
References( ino => ino.Author ).Column( "AuthorID" );
}
AuthorID字段是对Contact表的引用,尚未映射到Contact端(正如您从上一个Contact映射中看到的那样
答案 0 :(得分:5)
IssueNote.Author
Contact
类型是Fluent Mapping
吗?如果是,则References( ino => ino.Author ).Column( "AuthorID" );
ContactId
正在尝试更新您的“地址”表,以确保其AuthorId
存储在IssueNote.Author
的外键中,您告诉它Contact
在映射时使用的References( ino => ino.Author ).Column( "ContactId" );
{{1}}Š
你告诉过nHibernate:
当你真的想要这个时:
因此,将IssueNoteMap更改为以下内容,以便使用ContactId映射作者并使其正常工作
{{1}}