Automapping doesn't have an Id mapped

Gini picture Gini · Mar 29, 2010 · Viewed 8.3k times · Source
My Entity Class:
public class Building 
    {
        /// <summary>
        /// internal Id 
        /// </summary>
        public virtual long Id { get; set; }
..............
}

My Mapping:

var model = AutoMap.AssemblyOf<Building>()
                        .Setup(s => s.FindIdentity = p => p.Name == "Id")
                        .Where(t => t.Namespace == "SpikeAutoMappings");

var database = Fluently.Configure()
                        .Database(DatabaseConfigurer)
                        .Mappings(m=>m.AutoMappings.Add(model));

I need somebody to help me see what is wrong because I keep having this error when run unit test:

Initialization method TestProject1.MappingTestBase.TestInitialize threw exception. FluentNHibernate.Cfg.FluentConfigurationException:  FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 --->  FluentNHibernate.Visitors.ValidationException: The entity doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)..

Answer

J. Ed picture J. Ed · Feb 21, 2011

both answers above are right; unless you specify differently, the automapper assumes that you have an int Id field.
If your Id is long, the automapper might not recognize it correctly.
try defining a MappingOverride for your class(es), like so:

public class UserMappingOverride : IAutoMappingOverride<User>
{
    #region IAutoMappingOverride<User> Members

    public void Override(AutoMapping<User> mapping)
    {
        mapping.Id(u => u.Name);
    }

    #endregion
}

the Id() function allows you to override the automapper's convention of what the ID field should be.
for further info on overriding, see http://wiki.fluentnhibernate.org/Auto_mapping#Overrides.
Cheers,
Jhonny