BUG?: Id Column Name Not Mapping - Reproducible Example

Jeff Barriault's Avatar

Jeff Barriault

07 Sep, 2010 04:35 PM via web

I've managed to revise the Examples.FirstAutomappedProject so that this problem can be reproduced.

Modify the Employee.cs file so that it looks like this:

namespace Examples.FirstAutomappedProject.Entities
{
    public class Employee : EmployeeBase
    {
        public override string Id
        {
            get
            {
                return base.Id;
            }
            set
            {
                base.Id = value;
            }
        }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual Store Store { get; set; }
    }

    public class EmployeeBase
    {
        public virtual string Id { get; set; }
    }
}

Add an EmployeeMap class that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Automapping;
using Examples.FirstAutomappedProject.Entities;

namespace Examples.FirstAutomappedProject.Overrides
{
    public class EmployeeMap : IAutoMappingOverride<Employee>
    {
        public void Override(AutoMapping<Employee> mapping)
        {
            mapping.Id(x => x.Id, "EmployeeID")
                .GeneratedBy.Assigned();
        }
    }
}

Change the employees in Program.cs to look like this:

                var daisy = new Employee { Id = "harrid", FirstName = "Daisy", LastName = "Harrison" };
                var jack = new Employee { Id = "torraj", FirstName = "Jack", LastName = "Torrance" };
                var sue = new Employee { Id = "walkts", FirstName = "Sue", LastName = "Walkters" };
                var bill = new Employee { Id = "taftbi", FirstName = "Bill", LastName = "Taft" };
                var joan = new Employee { Id = "popejo", FirstName = "Joan", LastName = "Pope" };

And change the CreateAutomappings method of Program.cs to look like this so it outputs the hbm.xml files and uses the overrides:

    static AutoPersistenceModel CreateAutomappings()
    {
        AutoPersistenceModel model = AutoMap.AssemblyOf<Employee>(new ExampleAutomappingConfiguration())
            .Conventions.Add<CascadeConvention>()
            .IgnoreBase<EmployeeBase>()
            .UseOverridesFromAssemblyOf<EmployeeMap>();

        string path = @"c:\HBM";
        model.WriteMappingsTo(path);

        return model;
    }

Here is the hbm.xml that running the program generates:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Examples.FirstAutomappedProject.Entities.Employee, Examples.FirstAutomappedProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Employee`">
    <id name="Id" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="assigned" />
    </id>
    <property name="FirstName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="FirstName" />
    </property>
    <property name="LastName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="LastName" />
    </property>
    <many-to-one cascade="all" class="Examples.FirstAutomappedProject.Entities.Store, Examples.FirstAutomappedProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Store">
      <column name="Store_id" />
    </many-to-one>
  </class>
</hibernate-mapping>

As you can see, the column name is Id and not EmployeeID as specified in the mapping override. I know that this has something to do with overriding the Id property from the EmployeeBase class, but I've included the .IgnoreBase() statement in the mappings so I wouldn't think this would be a problem. Can anyone shed some light on this and provide a solution? I'm not sure this is a bug, but I frequently override properties in my base class and need a solution. I appreciate any advice anyone can offer. Thanks!

    Reply to this discussion

    Preview Comments are parsed with Markdown. Help with syntax

    Attached Files

      You can attach files up to 10MB

      What is 14 minus 4?