I have the following classes
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; }
}
These classes have been mapped to the database using FluentNHibernate. These are the mapping classes
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 );
}
I am trying to load a considerable amount of contacts inside the database using these classes. My code that create these object can be logically explained as follow
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
I have no problem with all the descendant collection like Email, PhoneNumber except that I have a problem with Address. In fact when I try to insert an contact that has at least one address I get the following error
Could not insert collection: [GSLConverter.Entities.Contact.Addresses#3551][SQL: UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1]
Invalid column name 'AuthorID'
Instead of using ContactID is using AuthorID. Where does that AuthorID come from????
These are the queries that NHibernate execute on the server
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: this is the mapping of the class, the only one, that reference an AuthorID member
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" );
}
The AuthorID field is a reference to the Contact table that have not be still mapped on the Contact side (as you can see from the previous Contact mapping