Fluent nHibernate 动态属性名称

发布于 2024-11-08 18:51:30 字数 1699 浏览 5 评论 0 原文

我想让这个映射起作用:

    Map(x => x.First, "First_ID");
    Map(x => x.Second, "Second_ID")

    References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

当我想将实体插入数据库时​​它不起作用。它说: Invalid Index N for this SqlParameterCollection with Count=N 错误

我在这里找到了问题的答案: http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error。 aspx

但答案说我需要删除下面的代码,以使插入工作

  Map(x => x.First, "First_ID");
  Map(x => x.Second, "Second_ID")

问题是我无法删除映射,因为这样我的实体无法被获取从数据库中。然后它说它无法在表中找到第一列和第二列。 我明白为什么会发生这种情况,但是有什么方法可以在不删除映射的情况下解决问题吗? 只是为了更改此代码中的某些内容:

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

例如,不使用列名称的文字,但某些代码“知道”在此映射属性中 First 映射到“First_ID”而无需显式指定列名称?

PS我尝试过使用

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns(x => x.First, x => x.Second)
        .LazyLoad();

它不起作用。

删除映射的另一个解决方案是更改从数据库获取实体的查询,但在这种情况下,我需要添加额外的联接 - 这是愚蠢的,因为我在自己的表中有该列,为什么我需要进行联接然后添加当我并不完全需要这种加入时,会受到一些限制。

I want to make this mapping work:

    Map(x => x.First, "First_ID");
    Map(x => x.Second, "Second_ID")

    References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

It doesn't work when I want to insert the entity to the database. It says:
Invalid Index N for this SqlParameterCollection with Count=N error

I've found an answer for the question here:
http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error.aspx

But the answer says I need to remove below code, to make insert work

  Map(x => x.First, "First_ID");
  Map(x => x.Second, "Second_ID")

The problem is that I can't remove the mappings cause in that way my entity couldn't be fetched from the database. It than says that it cannot find First and Second columns in table.
I understand why this happens but is there some way to solve the problem without removing the mappings?
Just to change something in this code:

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns("First_ID", "Second_ID")
        .LazyLoad();

For example not to use literals for column names, but some code that "knows" that in this mapping property First is mapped to "First_ID" without specifying explicitly the name of the column?

P.S. I've tried to use

References(x => x.SomeProperty)
        .Access.AsCamelCaseField(Prefix.Underscore)
        .Nullable()
        .NotFound.Ignore()
        .WithColumns(x => x.First, x => x.Second)
        .LazyLoad();

It doesn't work.

The other solution for removed mappings is to change the query that gets the entity from db, but in this case i need to add extra join - it's stupid cause I have the column in my own table, why i need to make join and then add some restrictions when I don't exactly need this join.

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

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

发布评论

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

评论(1

从﹋此江山别 2024-11-15 18:51:30

您可以通过执行以下操作来指定属性不应更新:

Map(x => x.First, "First_ID").Not.Update();
Map(x => x.Second, "Second_ID").Not.Update();

或者,如果您想要拥有这些属性的唯一原因是在查询中使用它们。你可以
或者

Map(x => x.First, "First_ID").Access.None();
Map(x => x.Second, "Second_ID").Access.None();

更简单地删除这些属性并使用良好的老式 hbm 文件来映射您的实体并为您的属性指定 access="none" 。这样,您就可以通过 Criteria 和 HQL 使用它们,但实际上不需要存在于您的实体中。

You can specify that the properties should not be updateable by doing:

Map(x => x.First, "First_ID").Not.Update();
Map(x => x.Second, "Second_ID").Not.Update();

Or if the only reason you want to have these properties is to use them in queries. You can
either do

Map(x => x.First, "First_ID").Access.None();
Map(x => x.Second, "Second_ID").Access.None();

Or more simply remove those properties and use good old fashion hbm files to map your entities and specify access="none" for your properties. That way they will be available to you through Criteria and HQL, but won't actually need to exist in your entity.

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