6

我有两张桌子,位置和设施

它们映射到两个类,

public Location : Entity
{
   //properties
}

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

一切都很好,直到我把设施改成这个

public Facility : Location
{

}

现在我从 nHibernate 得到一个例外说

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

由于某种原因,它没有将表的复数名称创建到 sql 字符串中。

谢谢你的帮助!

编辑

这是我当前的 TableNameConvention

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

当 Facility 从 Entity 继承时,Facility 确实通过此方法运行。当它从 Location 继承时,它不会

编辑 2 想我会发布所有内容...... 数据库图

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<>));
    }
}
4

2 回答 2

9

你有约定吗?

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

        instance.Table(Inflector.Net.Inflector.Pluralize(typeName));
    }
}
于 2010-08-26T21:52:22.970 回答
1

这是一个老问题,但为了其他偶然发现这个问题的人寻找答案,您还可以创建一个使用 EF 附带的内置 PluralizationService 的约定:

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

    }
}
于 2013-05-28T17:07:51.667 回答