【问题标题】:fluent nhibernate automap inferring incorrect key流利的休眠自动映射推断不正确的键
【发布时间】:2010-08-30 12:21:17
【问题描述】:

在将 Fluent NHibernate 与自动映射结合使用时,我在使用 CreateCriteria 向条件查询中添加外连接时遇到问题。

这是我的实体 -

public class Table1 : Entity
    {
        virtual public int tb1_id { get; set; }
        virtual public DateTime tb1_date_filed { get; set; }
    .
    .
    .
        virtual public IList<Table2> table2 { get; set; }
    }

public class Table2: Entity
    {
        public virtual int tb2_id { get; set; }
        public virtual int tb2_seqno { get; set; }
        .
        .
        .
        public virtual Table2 table2 { get; set; }
    }

我尝试使用以下方法将外部联接添加到我的条件查询中 -

CreateCriteria("Table2", NHibernate.SqlCommand.JoinType.LeftOuterJoin);

但我收到一个错误 -

{"EIX000: (-217) Column (tbl1_id) not found in any table in the query (or SLV is undefined)."}

所以它似乎正在尝试自动设置第二个表的 id,但不知道将其设置为什么。有没有办法可以专门设置id?这是我的会议 -

var persistenceModel = AutoMap.AssemblyOf<Table1>()
                .Override<Table1>(c => {    
                    c.Table("case");
                    c.Id(x => x.id).Column("tbl1_id");
                })
                .Where(t => t.Namespace == "MyProject.Data.Entities")
                .IgnoreBase<Entity>();

希望这有点道理。感谢您的任何想法。

【问题讨论】:

    标签: nhibernate fluent-nhibernate


    【解决方案1】:

    您似乎已经回答了您自己的问题,所以我只想提出一些建议......

    fluent nhibernate 的优点之一是它遵循自动创建映射的约定。您的实体似乎与您的数据库表的名称非常相关。

    为了映射到不同的数据库约定,同时保持实体和列的理想名称,您可以使用一些自定义约定:

    public class CrazyLongBeardedDBATableNamingConvention
        : IClassConvention
    {
        public void Apply(IClassInstance instance)
        {
            instance.Table("tbl_" + instance.EntityType.Name.ToLower());
        }
    }
    
    public class CrazyLongBeardedDBAPrimaryKeyNamingConvention
        : IIdConvention
    {
        public void Apply(IIdentityInstance instance)
        {
            string tableShort = TableNameAbbreviator.Abbreviate(instance.EntityType.Name); 
            instance.Column(tableShort + "_id");
        }
    }
    
    class CrazyLongBeardedDBAColumnNamingConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            string name = Regex.Replace(
                instance.Name,
                "([A-Z])",
                "_$1").ToLower();
    
            var tableShort = TableNameAbbreviator.Abbreviate(instance.EntityType.Name); 
            instance.Column(tableShort + name);
        }
    }
    

    TableNameAbbreviator 是一个知道如何缩写表名的类。

    这些映射来自:

    public class Table1 : Entity
    {
        virtual public int Id { get; set; }
        virtual public DateTime DateFiled { get; set; }
    }
    

    到如下表:

    CREATE TABLE tbl_table1 {
        tbl1_id INT PRIMARY KEY
        tbl1_date_filed datetime
    }
    

    【讨论】:

      【解决方案2】:

      我为我的第一个表添加了一个 HasMany 选项来定义与我的第二个表的关系。然后我为我的第二个表添加了一个覆盖,它定义了该表的 id 列。

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        相关资源
        最近更新 更多