postgresql - 使用 NHibernate 映射复合键时出现问题(列已添加)

标签 postgresql fluent-nhibernate

NHibernate(以及 Hibernate)的新手,我正在努力解决一个复合关键问题。这是部分数据库设计的简化版本。

  table_a
 +---------------------+
 | * a_id varcha (10)  |                   table_z
 |   label varchar(50) |                 +----------------------+
 |                     +<----------------+ * a_id varchar(10)   |
 +---------------------+      +----------| * b_id varchar(10)   |
                              |   +------+ * c_id varchar(10)   |
  table_b                     |   |      |   name varchar(100)  |
 +---------------------+      |   |      |                      |
 | * b_id varcha (10)  |      |   |      +----------------------+
 |   label varchar(50) <------+   |
 |                     |          |
 +---------------------+          |
                                  |
  table_c                         |
 +---------------------+          |
 | * c_id varcha (10)  <----------+
 |   label varchar(50) |
 |                     |
 +---------------------+

这里的关键元素是table_z主键是表a、b、c的3个主键的组合(因此,它控制a、b和c的唯一组合)。它们还分别与 table_a、table_b 和 table_c 进行 FK。

现在,除了数据库设计考虑之外,是否有一种方法可以将其映射到 NHibernate。我的尝试导致堆栈跟踪提示“ArgumentException:列 'a_id' 已添加到此 SQL 生成器中”。谷歌搜索告诉我,问题是我在连接的两端使用相同的字段名称。我很惊讶这甚至是一个问题 - 或者我完全误解了这个问题..

这是 DDL (Postgresql)

CREATE TABLE test.table_a(
a_id varchar(10) primary key,
label varchar(50)
);

CREATE TABLE test.table_b(
b_id varchar(10) primary key,
label varchar(50)
);


CREATE TABLE test.table_c(
c_id varchar(10) primary key,
label varchar(50)
);

CREATE TABLE test.table_z(
a_id varchar(10),
b_id varchar(10),
c_id varchar(10),
name varchar(100)
);

-- add combined primary key on table_z
ALTER TABLE test.table_z ADD CONSTRAINT pk_z_combined
    PRIMARY KEY (a_id,b_id,c_id)
;

-- FK

ALTER TABLE test.table_z ADD CONSTRAINT FK_to_a
    FOREIGN KEY (a_id) REFERENCES test.table_a (a_id) ON DELETE No Action ON UPDATE No Action;

ALTER TABLE test.table_z ADD CONSTRAINT FK_to_b
    FOREIGN KEY (b_id) REFERENCES test.table_b (b_id) ON DELETE No Action ON UPDATE No Action;

    ALTER TABLE test.table_z ADD CONSTRAINT FK_to_c
    FOREIGN KEY (c_id) REFERENCES test.table_c (c_id) ON DELETE No Action ON UPDATE No Action;

这是 Fluent Hibernate C# 代码


using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pm
{

    class TestAMapping : ClassMap<TestA>
    {
        public TestAMapping()
        {
            Table("test.table_a");
            Id(x => x.Id, "a_id");
            Map(x => x.Label, "label");
        }
    }

    class TestBMapping : ClassMap<TestB>
    {
        public TestBMapping()
        {
            Table("test.table_b");
            Id(x => x.Id, "b_id");
            Map(x => x.Label, "label");
        }
    }

    class TestCMapping : ClassMap<TestC>
    {
        public TestCMapping()
        {
            Table("test.table_c");
            Id(x => x.Id, "c_id");
            Map(x => x.Label, "label");
        }
    }

    class TestZMapping : ClassMap<TestZ>
    {
        public TestZMapping()
        {
            Table("test.table_z");
            CompositeId()
                .KeyProperty(x => x.Aid, "a_id")
                .KeyProperty(x => x.Bid, "b_id")
                .KeyProperty(x => x.Cid, "c_id");
            Map(x => x.Name, "name");
            References(x => x.TestAObj).Column("a_id");
            References(x => x.TestBObj).Column("b_id");
            References(x => x.TestCObj).Column("c_id");

        }
    }


    class TestA
    {
        public virtual string Id { get; set; }
        public virtual string Label { get; set; }

    }
    class TestB
    {
        public virtual string Id { get; set; }
        public virtual string Label { get; set; }

    }
    class TestC
    {
        public virtual string Id { get; set; }
        public virtual string Label { get; set; }

    }

    class TestZ
    {
        public virtual string Aid { get; set; }
        public virtual string Bid { get; set; }
        public virtual string Cid { get; set; }
        public virtual string Name { get; set; }
        public virtual TestA TestAObj { get; set; }
        public virtual TestB TestBObj { get; set; }
        public virtual TestC TestCObj { get; set; }

        // https://stackoverflow.com/a/7919012/8691687
        public override bool Equals(object obj)
        {
            var other = obj as TestZ;

            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return this.Aid == other.Aid &&
                this.Bid == other.Bid && this.Cid == other.Cid;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hash = GetType().GetHashCode();
                hash = (hash * 31) ^ Aid.GetHashCode();
                hash = (hash * 31) ^ Bid.GetHashCode();
                hash = (hash * 31) ^ Cid.GetHashCode();

                return hash;
            }
        }
    }

}

相关堆栈跟踪


FluentNHibernate.Cfg.FluentConfigurationException
  HResult=0x80131500
  Message=An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  Source=FluentNHibernate
  StackTrace:
   at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
   at pm.dal.DAL.CreateSessionFactory(String connectionString) in C:\Users\Laptop\source\repos\pm\dal\DAL.cs:line 49
   at pm.dal.DAL..ctor(String connectionString) in C:\Users\Laptop\source\repos\pm\dal\DAL.cs:line 41
   at pm.Manager.Manager.Connect(String connectionString) in C:\Users\Laptop\source\repos\pm\Manager\Manager.cs:line 102
  (blah...)

Inner Exception 1:
MappingException: Unable to build the insert statement for class pm.TestZ: a failure occured when adding the Id of the class

Inner Exception 2:
ArgumentException: The column 'a_id' has already been added in this SQL builder
Parameter name: columnName

谁能告诉我我哪里犯了罪..

非常感谢

最佳答案

我只是看看我在项目中是如何做到的。不幸的是,我无法描述为什么这是要走的路;-)

public TestZMapping()
{
    Table("test.table_z");
    CompositeId()
        .KeyProperty(x => x.Aid, "a_id")
        .KeyProperty(x => x.Bid, "b_id")
        .KeyProperty(x => x.Cid, "c_id");
    Map(x => x.Name, "name");
    References(x => x.TestAObj).Column("a_id").Not.Insert().Not.Update();
    References(x => x.TestBObj).Column("b_id").Not.Insert().Not.Update();
    References(x => x.TestCObj).Column("c_id").Not.Insert().Not.Update();
}

关于postgresql - 使用 NHibernate 映射复合键时出现问题(列已添加),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540548/

相关文章:

c# - SQL Server Express 连接字符串/FluentNHibernate

unit-testing - 单元测试流畅的 NHibernate。需要帮助理解测试期间发生的异常

ruby-on-rails - Rails 5 如何保存 postgres 中两个日期时间之间的小时和分钟差异?

node.js - 如何在 Sequelize/Node.js 中设置沙箱帐户?

sql - 查询计划 : Is the order of JOINS important

postgresql - 在 plpgsgl 异常处理程序中获取违反的 fk 的名称

node.js - 如何动态更改查询的值

fluent-nhibernate - 使用web.config流利的nhibernate配置错误

c# - 如何将 CaSTLe Activerecord 转换为纯 NHibernate 或 Fluent NHibernate?

fluent-nhibernate - CompositeId导致无法编译映射文档错误