Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent nHibernate Automapping not creating Plural table name

I have two tables, Locations and Facilities

They map to two classes,

public Location : Entity
{
   //properties
}

public Facility : Entity
{
    public virtual Location Location { get; set; }
}

Everything works just dandy, until I change facility to this

public Facility : Location
{

}

Now I get an exception from nHibernate saying

NHibernate.ADOException was unhandled by user code
  Message=could not execute query
 InnerException: System.Data.SqlClient.SqlException
       Message=Invalid object name 'Facility'.

For some reason it is not creating the plural name of the table into the sql string.

Thanks for any help!

EDIT

This is my current TableNameConvention

public class TableNameConvention : IClassConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
    }
}

When Facility inherits from Entity, the Facility does run through this method. When it inherits from Location, it does not

Edit 2 Figured I'd post everything... Database diagram

public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{

    #region IAutoPersistenceModelGenerator Members

    public AutoPersistenceModel Generate()
    {
        var mappings = new AutoPersistenceModel();
        mappings.AddEntityAssembly(typeof(Person).Assembly).Where(GetAutoMappingFilter);
        mappings.Conventions.Setup(GetConventions());
        mappings.Setup(GetSetup());
        mappings.IgnoreBase<Entity>();
        mappings.IgnoreBase(typeof(EntityWithTypedId<>));
        mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

        return mappings;

    }

    #endregion

    private Action<AutoMappingExpressions> GetSetup()
    {
        return c =>
        {
            c.FindIdentity = type => type.Name == "Id";
        };
    }

    private Action<IConventionFinder> GetConventions()
    {
        return c =>
        {
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.ForeignKeyConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.HasManyConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.HasManyToManyConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.ManyToManyTableNameConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.PrimaryKeyConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.ReferenceConvention>();
            c.Add<BHP.DEC.Data.NHibernateMaps.Conventions.TableNameConvention>();
        };
    }

    /// <summary>
    /// Provides a filter for only including types which inherit from the IEntityWithTypedId interface.
    /// </summary>

    private bool GetAutoMappingFilter(Type t)
    {
        return t.GetInterfaces().Any(x =>
                                        x.IsGenericType &&
                                        x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
    }
}
like image 380
Jason More Avatar asked Aug 26 '10 21:08

Jason More


2 Answers

Have you set a convention?

public class TableNameConvention : IClassConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        string typeName = instance.EntityType.Name;

        instance.Table(Inflector.Net.Inflector.Pluralize(typeName));
    }
}
like image 182
David Avatar answered Sep 20 '22 00:09

David


This is an old question, but for the sake of others who stumble upon this looking for an answer, you can also create a convention that uses the built-in PluralizationService that comes with EF:

public class TableNameConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        string typeName = instance.EntityType.Name;
        instance.Table(PluralizationService.CreateService(CultureInfo.CurrentCulture).Pluralize(typeName));

    }
}
like image 30
Derek Greer Avatar answered Sep 22 '22 00:09

Derek Greer