nhibernate Fluent 映射复合 ID

发布于 2024-11-08 18:27:47 字数 1126 浏览 2 评论 0原文

我正在学习使用流畅的 nhibernate,但无法理解如何创建映射。我有一个具有多列主键的表,但我无法正确映射它。我有以下流畅的映射 -

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
    {
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .KeyProperty(x => x.Id_1, "Id_1")
                .KeyProperty(x => x.Id_2, "Id_2");

            mapping.References(x => x.otherEntity)
                .Column("JoinColumn");

            // Commented out to attempt to map to another entity on multiple columns
            //mapping.HasMany(x => x.thirdEntit)
            //    .KeyColumns.Add("thirdId_1", "thirdId_2")
            //    .Cascade.All();



        }
    }

我遇到的问题是复合 id 似乎不起作用。

这是生成的映射文件中的一个片段 -

<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>

然后我收到一个错误,指出在查询的任何表中都找不到列 (id)。

我是否错误地认为我的映射代码会生成正确的复合 ID?我是否遗漏了什么或做错了什么?

I am learning to use fluent nhibernate and am having trouble understanding how to create my mappings. I have a table that has a multi-column primary key, but I can't get it to map correctly. I have the following fluent mapping -

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
    {
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .KeyProperty(x => x.Id_1, "Id_1")
                .KeyProperty(x => x.Id_2, "Id_2");

            mapping.References(x => x.otherEntity)
                .Column("JoinColumn");

            // Commented out to attempt to map to another entity on multiple columns
            //mapping.HasMany(x => x.thirdEntit)
            //    .KeyColumns.Add("thirdId_1", "thirdId_2")
            //    .Cascade.All();



        }
    }

The problem that I am having is the composite id doesn't seem to be working.

Here is a snippet from the produced mapping file -

<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>

I then get an error that column (id) is not found in any table in the query.

Am I wrong in thinking that my mapping code will produce the correct composite ID? Am I missing something or doing something incorrectly?

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

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

发布评论

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

评论(1

瀞厅☆埖开 2024-11-15 18:27:47

建议(并且我已经取得了很多成功)如果您正在执行 CompositeKeys,请按照以下方式使用 ID 对象:

NHibernate 和复合键

在您的情况下,您将创建一个新类...

[Serializable]
public MyEntityIdentifier
{
   public virtual TYPE Id_1;
   public virtual TYPE Id_2;

   // You need to override Equals(MyEntityIdentifier), Equals(object obj) and GetHashCode()
}

然后您的映射变为

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
{
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .ComponentCompositeIdentifier(x => x.MyEntityIdentifier)
                .KeyProperty(x => x.MyEntityIdentifier.Id_1, Id_1, "Id_1")
                .KeyProperty(x => x.MyEntityIdentifier.Id_2, "Id_2");

            // snip
        }
}

您的类来自具有两个属性(Id_1 和 Id_2),而仅具有 MyEntityIdentifier。

然后,您在需要加载的地方使用 MyEntityIdentifier。

MyEntity entity = Session.Load<MyEntity>(new MyEntityIdentifier { Id_1 = Whatever, Id_2 = Whatever });

而且,如果您对此数据库有任何控制权,我强烈建议不要使用 CompositeKeys。虽然 NHibernate 肯定会处理它们,但它们确实带来了一些额外的心理复杂性和问题。

It's recommended (and I've had lots of success with) that if you're doing CompositeKeys you use an ID object as per:

NHibernate and Composite Keys

In your case you'd create a new class...

[Serializable]
public MyEntityIdentifier
{
   public virtual TYPE Id_1;
   public virtual TYPE Id_2;

   // You need to override Equals(MyEntityIdentifier), Equals(object obj) and GetHashCode()
}

Then your mapping becomes

public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
{
        public void Override(AutoMapping<MyEntity> mapping)
        {
            mapping.CompositeId()
                .ComponentCompositeIdentifier(x => x.MyEntityIdentifier)
                .KeyProperty(x => x.MyEntityIdentifier.Id_1, Id_1, "Id_1")
                .KeyProperty(x => x.MyEntityIdentifier.Id_2, "Id_2");

            // snip
        }
}

And your class goes from having two properties (Id_1 & Id_2) to just having a MyEntityIdentifier.

Then you use your MyEntityIdentifier where required to load..

MyEntity entity = Session.Load<MyEntity>(new MyEntityIdentifier { Id_1 = Whatever, Id_2 = Whatever });

And also if you have ANY control over this database I would STRONGLY recommend not using CompositeKeys. While NHibernate will definitely handle them they do come with a bit of additional mental complexity and problems.

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