Fluent nHibernate Automapping 不创建复数表名

发布于 2024-09-16 19:32:14 字数 3048 浏览 2 评论 0原文

我有两个表,位置和设施

它们映射到两个类,

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

当设施继承自实体时,设施确实通过此方法运行。继承Location时,不

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-09-23 19:32:14

您是否设置了约定

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

        instance.Table(Inflector.Net.Inflector.Pluralize(typeName));
    }
}

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));
    }
}
却一份温柔 2024-09-23 19:32:14

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

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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文