c# - 使用 3 个表的 Fluent NHibernate Dictionary<string, Entity>

标签 c# nhibernate fluent-nhibernate

我正在尝试将多对多表结构映射到字典,其中键在一个表中,值在另一个表中。

table Rows [Row_ID]

table Columns [Column_ID, Column_Name]

table Cells [Cell_ID, Row_ID, Column_Id, Cell_Value]

public class Row {
    public Row() {
        this.Cells = new Dictionary<string, Cell>();
    }

    public virtual long Id { get; protected set; }

    public virtual IDictionary<string, Cell> Cells { get; set; }
}

public class RowMap : ClassMap<Row> {
    public RowMap() {
        Table("Rows");
        Id(m => m.Id, "Row_Id").GeneratedBy.Identity();
        HasManyToMany<Column>(m => m.Cells).Table("Cells")
            .ParentKeyColumn("Row_Id").ChildKeyColumn("Column_Id")
            .AsMap<string>("Column_Name").Inverse().Cascade.All();
    }
}

public class Column {
    public virtual long Id { get; protected set; }

    public virtual string Name { get; set; }
}

public class ColumnMap : ClassMap<Column> {
    public ColumnMap() {
        Table("Columns");
        Id(m => m.Id, "Column_Id").GeneratedBy.Identity();
        Map(m => m.Name, "Column_Name").Not.Nullable();
    }
}

public class Cell {
    public virtual long Id { get; protected set; }

    public virtual string Value { get; set; }

    public virtual Column Column { get; set; }

    public virtual Row Row { get; set; }
}

public class CellMap : ClassMap<Cell> {
    public CellMap() {
        Id(m => m.Id, "Cell_Id").GeneratedBy.Identity();
        Map(m => m.Value, "Cell_Value").Not.Nullable();
        References(m => m.Column, "Column_Id");
        References(m => m.Row, "Row_Id");
    }
}

一切都可以正确编译,但是当我尝试访问 Cells 集合时,出现以下错误:

could not initialize a collection: [Sandbox.Row.Cells#1][SQL: SELECT cells0_.Row_Id as
Row1_1_, cells0_.Column_Id as Column2_1_, cells0_.Column_Name as Column3_1_,
column1_.Column_Id as Column1_8_0_, column1_.Column_Name as Column2_8_0_ FROM Cells
cells0_ left outer join Columns column1_ on cells0_.Column_Id=column1_.Column_Id WHERE
cells0_.Row_Id=?]

查看错误消息后,我发现查询正在尝试访问键“Column_Name”作为 Cells 表而不是 Columns 表中的字段。

关于我如何告诉 NHibernate 将其映射到正确的表的任何想法,或者我试图做的是不可能的?

最佳答案

列表从未在 hasmanytomany(中间表:Cells、EntityTable of Cells:Cells)中提及,因此根本无法引用它。我目前看到的唯一选择是更改字典开头。

public virtual IDictionary<Column, Cell> Cells { get; private set; }

HasMany(m => m.Cells)
    .Table("Cells")
    .KeyColumn("Row_Id")
    .AsEntityMap("Column_id")
    .Inverse().Cascade.All();

您可以持有一个从 column.Name 到列实例的全局字典,以保存往返或将列名非规范化到单元格表中。

关于c# - 使用 3 个表的 Fluent NHibernate Dictionary<string, Entity>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388227/

相关文章:

c# - 如何查看远程文件的修改时间

NHibernate - 用于管理 SessionFactory/Session 的良好完整工作帮助程序类

c# - NHibernate Query to json 结果出乎意料

c# - CreateQuery().list() 的 NHibernate 输出列/投影

unit-testing - 如何使用NHibernate有效地进行TDD?

c# - Nhibernate/Linq : NHibernate. QueryException : could not resolve property: Profile. : MyNamespace. MyObject 的类

nhibernate - 使用 Fluent NHibernate 生成表索引

c# - 为什么后台代理会加载冗余程序集?

c# - 用 ado.net db 连接填充 treeview

c# - 这个 Timer 会从内存中释放吗?