Quite simply I have a one-to-one relationship between Member and MemberRegistration. MemberID is the Identity primary key in Member. MemberID is the Primary key in MemberRegistration.
All I want to be able to do is member.Save() and for NH to then generate the new unique memberID for Member and save the Member and cascade save MemberRegistration with the same memberID that was generated for Member.
I obviously have a not-null database constraint on both the Member and MemberRegistration tables.
I have set the cascade to save-update and inverse is true but now I have run out of ideas
I am new to NH but If some1 could help with this id be very grateful
NH version : 2.2
Member Mapping documents:
Code:
<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2">
<class name="TO.DataAccess.NHibernateClasses.Member, TO.DataAccess" table="Member">
<id name="MemberID" type="System.Int32" column="MemberID" unsaved-value="0">
<generator class="native" />
</id>
<property name="Firstname" type="System.String" column="Firstname" not-null="true" />
<property name="Surname" type="System.String" column="Surname" not-null="true" />
<property name="Sex" type="System.String" column="Sex" not-null="true" />
<property name="Email" type="System.String" column="Email" not-null="true" />
<property name="Password" type="System.String" column="Password" not-null="true" />
<one-to-one name="MemberRegistration" class="TO.DataAccess.NHibernateClasses.MemberRegistration, TO.DataAccess" cascade="save-update" />
</class>
</hibernate-mapping>
MemberRegistration Mapping documents:Code:
<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2">
<class name="TheOdes.DataAccess.NHibernateClasses.MemberRegistration, TheOdes.DataAccess" table="MemberRegistration">
<id name="MemberID" type="System.Int32" column="MemberID" unsaved-value="0">
<generator class="native" />
</id>
<property name="LastLoginDate" type="System.DateTime" column="LastLoginDate" not-null="false" />
<property name="EmailConfirmed" type="System.Boolean" column="EmailConfirmed" not-null="true" />
<property name="AllowNewsletter" type="System.Boolean" column="AllowNewsletter" not-null="true" />
<property name="FailedAttempts" type="System.Int32" column="FailedAttempts" not-null="true" />
<one-to-one name="Member" class="TheOdes.DataAccess.NHibernateClasses.Member, TheOdes.DataAccess" cascade="none" />
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
[SqlException (0x80131904): Cannot insert the value NULL into column 'MemberID', table 'TheOdes.dbo.MemberRegistration'; column does not allow nulls. INSERT fails.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +862234
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +739110
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1956
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +31
System.Data.SqlClient.SqlDataReader.get_MetaData() +62
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +903
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +122
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +9
NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd) +68
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) +531
[ADOException: could not insert: [TheOdes.DataAccess.NHibernateClasses.MemberRegistration][SQL: INSERT INTO MemberRegistration (SignUpDate, LastLoginDate, EmailConfirmed, AllowCRMEmails, AllowNewsletter, FailedAttempts, RegistrationStatusID, ValidationStageID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]]
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) +1140
NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session) +359
NHibernate.Impl.ScheduledIdentityInsertion.Execute() +100
NHibernate.Impl.SessionImpl.Execute(IExecutable executable) +169
NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) +929
NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) +526
NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything) +426
NHibernate.Impl.SessionImpl.Save(Object obj) +279
NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) +476
NHibernate.Engine.ActionSaveUpdateClass.Cascade(ISessionImplementor session, Object child, Object anything) +52
NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, Object child, IType type, CascadingAction action, CascadeStyle style, CascadePoint cascadeTo, Object anything) +264
NHibernate.Engine.Cascades.Cascade(ISessionImplementor session, IEntityPersister persister, Object parent, CascadingAction action, CascadePoint cascadeTo, Object anything) +320
NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) +1198
NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything) +526
NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything) +426
NHibernate.Impl.SessionImpl.Save(Object obj) +279
NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) +476
TheOdes.DataAccess.NHibernateClasses.Member.Save() in C:\Projects\TheOdes\TheOdes.DataAccess\NHibernateClasses\Member.cs:20
SignUp.Register() in c:\Projects\TheOdes\TheOdes.UI.Web\SignUp.aspx.cs:116
SignUp.btnSubmitMember_Click(Object sender, EventArgs e) in c:\Projects\TheOdes\TheOdes.UI.Web\SignUp.aspx.cs:68
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
Name and version of the database you are using:
Sql Server 2005