代码之家  ›  专栏  ›  技术社区  ›  czuroski

fluent nhibernate automap推断不正确的密钥

  •  0
  • czuroski  · 技术社区  · 16 年前

    这是我的实体-

    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)."}
    

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

    希望这有点道理。谢谢你的建议。

    2 回复  |  直到 16 年前
        1
  •  1
  •   joshperry    16 年前

    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
  •  0
  •   czuroski    16 年前

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

    谢谢