Tomas Aschan
Tomas Aschan

Reputation: 60674

NHibernate not updating all properties of entities

I'm experiencing an odd problem with FluentNHibernate: when I save my entity, one of the (reference) properties is not updated. Other properties, both fields and references, are updated, and the failing property is correctly mapped (retrieving entities works like a charm).

A (slightly simplified) description of what I'm doing:

  1. Into my MVC action method, an InputModel is bound and set. It has a property for the TypeID, where I wish to set the Type of my entity (let's call the entity type Thing).
  2. A new Thing object is created, and the simple properties of the InputModel is copied over. For a couple of complex properties, among them the Type property which isn't working and another property which is, the following is done:
    2.1. The correct ThingType is fetched from the repository, based on the provided type id.
    2.2. The type is set (using thing.Type = theType) on the new Thing object.
  3. The Thing that I want to update is fetched from the repository, based on the id on the input model (not the same id as the TypeID).
  4. All properties, complex and other, are copied over from the new thing (created by me) to the original one (fetched from db).
  5. The original Thing is saved, using session.Save();.

As stated above, it's only one property that isn't working - other properties, following (as far as I can tell) the exact same pattern, work. I've also debugged and verified that the original Thing has the correct, updated Type when it is passed to session.Save().

I have no idea where to start troubleshooting this...

Update: The classes are plain POCOs:

public class Thing 
{
    public int ID { get; set; }
    public string SomeSimpleProp { get; set; }
    public ThingType Type { get; set; }
    public OtherEntity OtherReference { get; set; }
}
public class ThingType
{
    public int ID { get; set; }
    public string Name { get; set; }
}

My exact mappings (except for the names of types and properties) are these:

// In ThingMap : ClassMap<Thing> constructor:
Id(t => t.ID).Column("ThingID");
Map(t => t.SomeSimpleProp);
References(t => t.Type).Column("ThingTypeID");
References(t => t.OtherReference).Column("OtherReferenceID");

// In ThingTypeMap : ClassMap<ThingType> constructor:
Id(t => t.ID).Column("ThingTypeID");
Map(t => t.Name);

As I said, OtherReference is updated correctly while Type is not. They are mapped identically, so I don't see how this could be a mapping error.

Upvotes: 1

Views: 974

Answers (1)

Felice Pollano
Felice Pollano

Reputation: 33272

You should specify <many-to-one .... cascade="save-update"/> in order to update references.

Upvotes: 1

Related Questions