Okay so I have the following mapping files, I've missed a lot of the columns out as they are mapping successfully:
public sealed class CustomerMap : ClassMap<Customer>, IMap
{
public CustomerMap()
{
WithTable("customers");
Not.LazyLoad();
Id(x => x.Id).GeneratedBy.Increment();
Map(x => x.Username);
...
...
...
HasOne(x => x.CustomerNew).Cascade.All().WithForeignKey("Username").;
HasOne(x => x.CustomerSurvey).Cascade.All().WithForeignKey("Username");
}
}
public sealed class CustomerNewMap : ClassMap<CustomerNew>, IMap
{
public CustomerNewMap()
{
WithTable("customers_NEW");
Not.LazyLoad();
Id(x => x.Username).GeneratedBy.Assigned();
}
}
public sealed class CustomerSurveyMap : ClassMap<CustomerSurvey>, IMap
{
public CustomerSurveyMap()
{
WithTable("customer_survey");
Not.LazyLoad();
Id(x => x.Username).GeneratedBy.Assigned();
}
}
And the entities look like this:
public class Customer : IEquatable<Customer>
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual CustomerNew CustomerNew { get; set; }
public virtual CustomerSurvey CustomerSurvey { get; set; }
}
So when a Customer entity is saved via NH an 'INSERT' statement is generated for the Customer entity but for the CustomerSurvey & CustomerNew entities 'UPDATE' statements are generated which causes the save to fail.
What could be wrong with the mapping?
NH debug sql output taht is failing:
NHibernate: INSERT INTO customers (Username, Password, Company, FirstName, LastName, JobTitle, Dept, Userphone, Userfax, email_address, City, Country, Comments, doc_author, usage_counter, last_research_accessdate, sales_id, salesperson_guess, business_sector, service_level, disclaimer_accepted, disclaimer_accepted_date, tech_contact, TechPhone, comments_support, web_browser, web_access, web_access_guess, Platform, modification_date, revision_no, ClientStatus, ReferTo, marketing_mailshot, marketing_mailshot_id, salesnav_transfer, creation_date, approval_date, registration_status, investor_category_id, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28, @p29, @p30, @p31, @p32, @p33, @p34, @p35, @p36, @p37, @p38, @p39, @p40); @p0 = 'Test-bed93f34-c', @p1 = 'rbsfm', @p2 = 'Test-bed93f34-c', @p3 = 'Test-bed93f34-c', @p4 = 'Test-bed93f34-c', @p5 = 'Test-bed93f34-c', @p6 = 'Test-bed93f34-c', @p7 = 'Test-bed93f34-c', @p8 = '', @p9 = 'Test-bed93f34-c', @p10 = 'Test-bed93f34-c', @p11 = 'Tes', @p12 = '', @p13 = '0', @p14 = '', @p15 = '', @p16 = '-1', @p17 = 'Test-bed93f34-c', @p18 = '', @p19 = '', @p20 = '1', @p21 = '27/08/2009 14:03:14', @p22 = '', @p23 = '', @p24 = '', @p25 = '', @p26 = '', @p27 = '', @p28 = '', @p29 = '', @p30 = '', @p31 = '7', @p32 = '218', @p33 = '', @p34 = '', @p35 = '', @p36 = '27/08/2009 14:03:14', @p37 = '', @p38 = 'USR', @p39 = 'rbs', @p40 = '7561'
NHibernate: UPDATE customers_NEW SET Company = @p0, Firstname = @p1, Lastname = @p2, JobTitle = @p3, Dept = @p4, Userphone = @p5, email_address = @p6, City = @p7, Country = @p8, SalesPerson = @p9, investor_category_id = @p10 WHERE Username = @p11; @p0 = 'Test-bed93f34-c', @p1 = 'Test-bed93f34-c', @p2 = 'Test-bed93f34-c', @p3 = 'Test-bed93f34-c', @p4 = 'Test-bed93f34-c', @p5 = 'Test-bed93f34-c', @p6 = 'Test-bed93f34-c', @p7 = 'Test-bed93f34-c', @p8 = 'Tes', @p9 = 'Test-bed93f34-c', @p10 = 'rbs', @p11 = 'Test-bed93f34-c'
Thanks in advance
Ollie