Your error message explains this pretty clearly:
System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'FloorPlanName', table 'Orchard.dbo.CommunityWebsiteSolutions_ApartmentPartRecord'; column does not allow nulls. INSERT fails.
Your problem occurs because:
- You are using nullable types such as
string
andint?
types in your Record class, which means you want to allow nulls. - Yet, you are specifying in your DB migration that you want to disallow nulls.
- And when C# instantiates your Record class, it initializes the fields using the default value, which is null for nullable types.
You can do one of the following:
- Make your DB columns nullable (remove
NotNull
) - Make your Record class use non-nullable types (for example,
int
instead ofint?
). Note that this is not an option for reference types such asstring
. - Give non-null default values to the fields of your Record class by giving the class a constructor. This is arguably bad practice since you will be calling virtual properties in a base class, but seems to be ok in NHibernate.
- Give non-null default values to the fields of your Record class by giving your part an
OnInitializing
handler, which would be placed in your Handler class.
UPDATE
You commented that you are expecting the fields to be filled in by the TryUpdateModel
in the Editor
function of your driver class. This does eventually happen, but the actual sequence of events that occurs is this (you can see this in the CreatePOST
method of Orchard.Core.Contents.Controllers.AdminController
):
ContentManager.New()
with the content type ID to create content item in memory. This step callsOnInitializing
for the appropriate content parts for the content type, which are defined in handlers.ContentManager.Create()
with the content item in Draft Mode. This step actually tries to persist the item to the DB once.ContentManager.UpdateEditor()
. This is the call that actually callsEditor
of the appropriate driver for the content type.- Check the
ModelState
and roll back the transaction if anything has failed.
Step 2 will fail if you have NULL values in columns marked NotNull
, because the fields have default values at that point. For these columns, you have to fill them in before step 2 by using OnInitializing
or by using a constructor on your Record part.
In other words, TryUpdateModel
in your driver is actually applying changes directly to the entity that has already been Create
d and is now attached to the NHibernate session.