I think it is a problem of inverse.. thing. You HasMany should have an "Inverse=True"-
OTOH (but not related to the problem); I think a better design will do these two operations as a single thing in the same Client operation: > email.CustomerID = customer; > customer.Emails.Add(email); Something like customer.AddMail(email); Then... I think this should be done in the constructor of the Customer class: > customer.Emails = new List<Email>(); regards, 2011/5/18 Michael <[email protected]> > I am having an issue with ActiveRecord erroring out when saving. I > know active record should be able to handle this scenario as I've > mocked it up in a simple demo project and it works just fine. But in > my application it does not work. Here's a simplified scenario. > > I have two tables, Customer and Email. Customer has an ID (sql server > identity field), a First (string) and Last (string) fields. The Email > table has an EmailID (sql server identity field), CustomerID (foreign > key referencing the customer table) and Address (string). > > Here's the simplified Customer class: > > [ActiveRecord("Customer")] > public class Customer : ActiveRecordBase<Customer>, ICustomer > { > [PrimaryKey(Generator = PrimaryKeyType.Identity, Column = > "CustomerID")] > public virtual int ID { get; set; } > > [Property] > public virtual string First { get; set; } > > [Property] > public virtual string Last { get; set; } > > [HasMany(typeof(Email), Cascade = > ManyRelationCascadeEnum.AllDeleteOrphan, Table = "Email", ColumnKey = > "CustomerID")] > public virtual IList<Email> Emails { get; set; } > } > > And the email class: > [ActiveRecord("Email")] > public class Email : ActiveRecordBase<Email>, IEmail > { > [PrimaryKey(Generator = PrimaryKeyType.Identity, Column = > "EmailID")] > public virtual int ID { get; set; } > > [BelongsTo(Column = "CustomerID")] > public virtual Customer CustomerID { get; set; } > > [Property] > public virtual string Address { get; set; } > > } > > In a simple demo project, attempting something like the following > works just fine: > > var customer = new Customer(); > customer.First = "First"; > customer.Last = "Last"; > customer.Emails = new List<Email>(); > > var email = new Email(); > email.Address = "[email protected]"; > email.CustomerID = customer; > customer.Emails.Add(email); > customer.Save(); > > But, in production (more complicated scenario but essentially doing > the same thing) it errors out. An error similar to this gets thrown: > > Castle.ActiveRecord.Framework.ActiveRecordException: Could not perform > Update for Customer ---> NHibernate.Exceptions.GenericADOException: > could not insert: [****.***.Email][SQL: INSERT INTO Emails (Address, > CustomerID) VALUES (?, ?); select SCOPE_IDENTITY()] ---> > System.Data.SqlClient.SqlException: Cannot insert the value NULL into > column 'CustomerID', table '****.***.Email'; column does not allow > nulls. INSERT fails. > The statement has been terminated. > > So, obviously AR thinks the Customer object hasn't been saved yet and > therefore it doesn't have an ID value to put into the CustomerID field > in the Emails table. I could set this column to allow NULLS but this > doesn't seem the right approach. > > Does anyone have an idea why AR would choke in a scenario like this? > Like I said, I am able to do this very easily with a simple dummy mock > up. But, in production, where things are more complicated, it is > failing. Any thoughts/help appreciated! > > -- > You received this message because you are subscribed to the Google Groups > "Castle Project Users" group. > To post to this group, send email to [email protected] > . > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/castle-project-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.
