nhibernate:在多通属性中使用compositeId的部分

nhibernate:在多通属性中使用compositeId的部分,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我有这样一个表结构: Table entity ( otherEntity_id int // primarykey id int // primarykey parent_id int ) 和阶级 public class Entity { public OtherEntity Other { get; set; } // simple int to discriminate different Entit

我有这样一个表结构:

Table entity
(
    otherEntity_id  int  // primarykey
    id              int  // primarykey

    parent_id       int
)
和阶级

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public Entity Parent { get; set; }
}
是否可以将其映射到类实体?(如果是,如何进行)

由于dbcommand中没有足够的列,因此在save时抛出以下命令,其中一个命令使用了两次:

        CompositeId()
            .KeyReference(e => e.Other, "otherEntity_id")
            .KeyProperty(e => e.Id, "id")

        References(e => e.Parent).Columns("otherEntity_id", "parent_id");
使用xml或fluent并不重要

编辑: 无引用映射无错误,引用映射有以下错误 (每次我的值多于映射列时,我都会多次出现此错误):

测试代码

        var e1 = new Entity { Id = 2, Other = other };
        var e2 = new Entity { Id = 3, Other = other, Parent = e1 };

        Session.Save(e1);
        Session.Save(e2);
        Session.Flush();    // throws here
        Session.Clear();

        var key = new Entity { Id = e2.Id, Other = e2.Other };

        var loaded = Session.Get<Entity>(key);
var e1=新实体{Id=2,Other=Other};
var e2=新实体{Id=3,Other=Other,Parent=e1};
Session.Save(e1);
Session.Save(e2);
Session.Flush();//扔到这里
Session.Clear();
var key=newentity{Id=e2.Id,Other=e2.Other};
var loaded=Session.Get(键);
编辑:


如果不可能,请有人告诉我好吗?

如果您两次引用上面的同一列(otherEntity\u id),我见过这样的映射以避免此类错误:

References(e => e.Parent).Columns("otherEntity_id", "parent_id")
    .Not.Update()
    .Not.Insert();

此外,当您遇到问题时,显示您遇到的完整错误消息总是很有帮助的。

我仍然没有找到解决方案,并将更改后的模型映射为:

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public int ParentId { get; set; }
}

我不知道从非自由主义者的角度看,多对一的关系在哪里存在。实体类只能包含一个父引用和一个其他实体引用。请提供信息以澄清关系。OtherEntity有一个实体集合,每个实体都有一个父实体(如果它是从该集合派生的)。要引用(多个)其父项,实体必须持有实体的主键(otherEntity_id,id),但在实体表中,它使用自己的主键部分otherEntity_id而不是另一列父项otherEntity_id。为什么要使用复合id?@Jakub Linhart,因为id本身不是唯一的。好主意,不幸的是,这会阻止插入两个列(检查sql),但我只需要忽略第一个列
public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public int ParentId { get; set; }
}