Fluent nHibernate 表中没有标识列

发布于 2024-08-02 21:59:21 字数 398 浏览 6 评论 0原文

如何为没有标识列的表指定流畅的 NHibernate 映射?

我想要这样的东西:

public sealed class CustomerNewMap : ClassMap<CustomerNew>, IMap
{
    public CustomerNewMap()
    {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Not.Id(); // this is invalid...
        Map(x => x.Username);
        Map(x => x.Company);
    }
}

我的意思是数据库中没有定义主键(数据库中没有太多定义)。

How do I specify with fluent NHibernate mapping for a table that doesn't have an identity column?

I want something like this:

public sealed class CustomerNewMap : ClassMap<CustomerNew>, IMap
{
    public CustomerNewMap()
    {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Not.Id(); // this is invalid...
        Map(x => x.Username);
        Map(x => x.Company);
    }
}

I mean no primary key defined in the database (not much defined in the database).

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

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

发布评论

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

评论(3

生来就爱笑 2024-08-09 21:59:21

我发现答案是:

  public CustomerNewMap()
  {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Id(x => x.Username).GeneratedBy.Assigned();
        Map(x => x.Company);
  }

I found the answer to be:

  public CustomerNewMap()
  {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Id(x => x.Username).GeneratedBy.Assigned();
        Map(x => x.Company);
  }
泛滥成性 2024-08-09 21:59:21

这并没有直接回答问题..但它回答了此处已接受答案的评论中的问题。

我们的 Ledger 上的 SQL 视图也遇到了同样的问题。问题是当 Id 属性不唯一时,NHibernate 会很高兴地重复行。在我们的例子中......我们有将 CustomerAccountId 设置为 Id 的 Ledger 条目......这在视图中不是唯一的。
为了解决这个问题......我映射了一个 CompositeId,它基于我能找到的使该行唯一的任何内容。在我们的例子中,它是 CustomerAccountIdWeekEnding 的组合:

CompositeId()
    .KeyProperty(x => x.CustomerAccountId)
    .KeyProperty(x => x.WeekEnding);

这足以让 NHibernate 不映射重复项。

This doesn't directly answer the question.. but it answers the issue in the comment to the accepted answer here.

We had the same issue with a SQL View over our Ledger. The problem is that NHibernate will happily duplicate rows when the Id property isn't unique. In our case.. we had Ledger entries that had the CustomerAccountId set as the Id.. which wasn't unique within the view.
To get around that.. I mapped a CompositeId that was based on whatever I could find that made the row unique. In our case, it was a combination of CustomerAccountId and WeekEnding:

CompositeId()
    .KeyProperty(x => x.CustomerAccountId)
    .KeyProperty(x => x.WeekEnding);

This was enough to have NHibernate not map duplicates.

街角卖回忆 2024-08-09 21:59:21

我发现在类似的情况下我必须显式设置列名称。像这样的东西

  Id(x => x.Username).Column("Username").GeneratedBy.Assigned();

I found I had to explicitly set a column name in a similar case. Something like

  Id(x => x.Username).Column("Username").GeneratedBy.Assigned();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文