NHibernate 使用 Fluent 引用子类映射

发布于 2024-12-13 04:02:32 字数 1987 浏览 1 评论 0原文

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

例外

  • 无法在表中为标识列插入显式值
  • 无法将标识列键生成与联合子类映射一起使用
  • 为 XXX 类生成 null id

当我有一个确实保存的映射时,我遇到以下问题

我已成功在数据库中插入和更新记录,但这些记录没有适当的 ID。它们的 id 都为 0,因此只会一遍又一遍地更新相同的记录。

我试图解决的问题

我正在尝试SubclassMap接口IRequest。此接口用作单独类 AbstractWorkflowRequestInformation 上的属性。保存父类时,我想将引用的IRequest保存在适当的子类表中。这是我当前的映射,它生成异常无法在表中插入标识列的显式值。我确信我在映射这两个类之间的关系的方式上进行了一些调整。我做错了什么?我的地图如下。

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
    }
}

I have tried many different mapping configurations but continue to generate exceptions or create / update the wrong record.

Exceptions

  • Cannot insert explicit value for identity column in table
  • Cannot use identity column key generation with union-subclass mapping
  • null id generated for class XXX

When I have a map that does save I have the following problem

I have successfully inserted and updated records in the database but these records do not have the appropriate id. They all have 0 for an id and as such only update the same record over and over.

Problem I am trying to solve

I am attempting to SubclassMap the interface IRequest. This interface is used as a property on a separate class AbstractWorkflowRequestInformation. When saving the parent class I want to save the referenced IRequest in the appropriate sub-class table. This is my current mapping which generates the exception Cannot insert explicit value for identity column in table. I am sure I have something tweaked in the way I am mapping the relationship between these two classes. What am I doing wrong? My maps are below.

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-12-20 04:02:32

这里的问题是我的 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();
    }
}

The issue here was that my IRequest map was missing a constraining reference back to AbstractWorkflowRequestInformation. So by adding a WorkflowRequestInformation property and the constraint everything now works as I intended.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文