views:

266

answers:

2

Ok, so I have an object named "Business" (I know, I know, "Business Business Object") When it saved its foreign keys are getting lost somehow. Everything else is saving. I have stepped through the code and followed the object up until the

session.SaveOrUpdate(businessObject);

method. The object at that point is intact and the foreign properties seem to be there. However when it is saved the foreign IDs are not in the Business Table. I can only imagine this may be due to me not mapping them correctly?

Here is where they are being mapped:

<many-to-one insert="false" update="false" name="_Township" column="TownshipId" class="HQData.Objects.Township, HQData" />

    <many-to-one insert="false" update="false" name="_User" column="UserId" class="HQData.Objects.User, HQData" />

    <many-to-one insert="false" update="false" name ="_SubCategory" column="SubCategoryId" class="HQData.Objects.SubCategory, HQData"/>

Here is where the object is being loaded and saved:

HQData.Objects.Business business = null;
      if (BusinessId != 0)
       business = BusinessManager.GetBusiness(BusinessId);
      else
       business = new HQData.Objects.Business();
         business._SubCategory = CategoryManager.GetCategory(Convert.ToInt32(ddlCategory.SelectedValue.Trim()));
      business.Name = ControlHelper.Sanitize(txtName.Text.Trim());
         business.Address1 = ControlHelper.Sanitize(txtAddress.Text.Trim());
         business._Township = TownshipManager.GetTownshipByCityorZip(ControlHelper.Sanitize(txtTownship.Text));
         business.PhoneNumber = ControlHelper.Sanitize(txtPhone.Text.Trim());
         business.FaxNumber = ControlHelper.Sanitize(txtFax.Text.Trim());
         business.Email = ControlHelper.Sanitize(txtEmail.Text.Trim());
         business.Website = ControlHelper.Sanitize(txtWebsite.Text.Trim());
         business.AboutUs = ControlHelper.Sanitize(txtAboutUs.Text.Trim());
         business._User = UserManager.GetCurrentUser();


      BusinessManager.SaveBusiness(business);

Thank you for your help.

A: 

Did you mean business._SubCategory.SubCategoryId

and not sure what GetCurrentUser returns but if Id as well...

business._User.UserId?

rball
the object gets mapped in the Business.cs file as: public virtual User _User { get; set; }so, I don't think I can set the ID value, I set the entire object, no?
Sara Chipps
Yes, it just looks like you are setting an int to the SubCategory instead of a SubCategory object. Don't set the Id value, instead set the SubCategory to the object.
rball
Crap nevermind, just saw the CategoryManager.GetCategory around the int stuff. Oops.
rball
+1  A: 

Set the insert update properties to true or remove them.

JoshBerke