NHibernate使用Fluent引用子类映射

时间:2011-10-31 23:08:31

标签: nhibernate fluent-nhibernate subclass

我尝试了许多不同的映射配置,但继续生成异常或创建/更新错误的记录。

例外

  • 无法在表格
  • 中插入标识列的显式值
  • 不能使用带有union-subclass映射的标识列密钥生成
  • 为类XXX生成的null id

当我有一张保存的地图时,我遇到以下问题

我已成功插入并更新了数据库中的记录,但这些记录没有相应的ID。对于id,它们都有0,因此只能一遍又一遍地更新相同的记录。

问题我正在尝试解决

我正在尝试SubclassMap界面IRequest。此接口用作单独的类AbstractWorkflowRequestInformation上的属性。保存父类时,我想将引用的IRequest保存在适当的子类表中。这是我当前的映射,它生成异常Cannot insert explicit value for identity column in table。我确信我在映射这两个类之间关系的方式上有一些调整。我究竟做错了什么?我的地图在下面。

IRequestMap

public class IRequestMap : ClassMap<IRequest>
{
    public IRequestMap()
    {
        Id(x => x.WorkflowRequestInformation)
            .GeneratedBy.Foreign("AbstractWorkflowRequestInformation");
        UseUnionSubclassForInheritanceMapping();
    }
}

public class PlanRequestMap : SubclassMap<PlanRequest>
{
    public PlanRequestMap()
    {
        Table("plan_request");
        // specific to PlanRequest property mappings and references
    }
}

public class BnjRequestMap : SubclassMap<BnjRequest>
{
    public BnjRequestMap()
    {
        Table("scratchdb.guest.bnj_request");
        // specific to BnjRequest property mappings and references
    }
}

AbstractWorkflowRequestInformationMap

public class AbstractWorkflowRequestInformationMap :
    ClassMap<AbstractWorkflowRequestInformation>
{
    public AbstractWorkflowRequestInformationMap()
    {
        Table("workflow_request_information");
        Id(x => x.Id)
            .Column("workflow_request_information_id")
            .GeneratedBy.Identity();

        // more property mappings and references
        References(x => x.SuperDuperRequest, "workflow_request_information_id")
            .Class<IRequest>().Unique().Cascade.All();
        // more property mappings and references
    }
}

1 个答案:

答案 0 :(得分:0)

这里的问题是我的IRequest地图缺少对AbstractWorkflowRequestInformation的约束引用。因此,通过添加WorkflowRequestInformation属性和约束,现在一切都按照我的意图运行。

public class IRequestMap : ClassMap<IRequest>
{
    public IRequestMap()
    {
        // adding this fixed the problem
        HasOne(x => x.WorkflowRequestInformation).Constrained();
        // adding this fixed the problem

        Id(x => x.WorkflowRequestInformation)
            .GeneratedBy.Foreign("AbstractWorkflowRequestInformation");
        UseUnionSubclassForInheritanceMapping();
    }
}
相关问题