Search code examples
nhibernatefluent-nhibernatesql-updatefluent-nhibernate-mapping

I can not update a record with fluent nhibernate 3 and map with generatedby.assigned generator


I have a design with fluent n hibernate, I use nhibernate linq to query the database. The app use a domain driver architecture. The database is MSSQL 2012. I don't have problem to insert, update or delete with any table except the case of one table that use ID without seed identity. then I need to use in the map the following:

 public class SySchoolLogoMap : ClassMap<SySchoolLogo>
    {
        public SySchoolLogoMap()
        {
            ReadOnly();

            Table("SySchoolLogo");

            Id(x => x.ID).Column("ImgId").GeneratedBy.Assigned();
            Map(x => x.ContentType).Column("ContentType").Nullable();
            Map(x => x.ImagenBytes).Column("Image").Not.Nullable().Length(50000);
            Map(x => x.ImgLenth).Column("ImgLen").Nullable();
            Map(x => x.ImageFile).Column("imgFile").Nullable();
            Map(x => x.OfficialUse).Column("OfficialUse").Nullable();
            Map(x => x.ImageCode).Column("ImageCode").Nullable();
            Map(x => x.Description).Column("Description").Nullable();
        }
}

The domain is as following:

 public class SySchoolLogo : DomainEntityWithTypedID<int>
{
  ... abbreviate
}

The base DomainEntityWithTypedID has only a ID integer that is used as Table primary key.

The update operation is using nhibernate link, I I am sure that the service is called and executed.

The update service is the following:

[HttpPost]
        public HttpResponseMessage UpdateLogo([FromBody]SchoolLogoOutputModel logodata)
        {
            try
            {
                var logo = repository.Get<SySchoolLogo>(logodata.ID);
                if (logo == null)
                {
                    throw new HttpBadRequestResponseException("The Image does not exists or was erased in server.");
                }
               logo.UpdateLogo(logodata.Description, logodata.ImageCode, logodata.OfficialUse);

                repository.SaveAndFlush(logo);
                 return new HttpResponseMessage(HttpStatusCode.OK);
            } //catch ignored to abbreviate.

I had debugged the procedure and I am sure that the operation of Save was executed, also I tried with Update, Merge and all flush variant. The operation return as OK, but the database is not updated. I am missing something, but I can not find what is, any help?


Solution

  • Well, you are using 5.1.3. class mapping:

    <class
        ...
        mutable="true|false"                          (4)
        ...
    

    (4) mutable (optional, defaults to true): Specifies that instances of the class are (not) mutable.

    which is the above mapping in fluent:

    public SySchoolLogoMap()
    {
        ReadOnly(); // mutable="false"
    

    And that would be the reason why NHiberante correctly does NOT execute any UPDATE...