首页 > 解决方案 > nHibernate HasMany 与连接表

问题描述

我正在尝试使用 Join 表创建一对多关系,但我很难做到这一点。下面是 C# 中的表结构、实体和映射

CREATE TABLE tbl_configurations
(
    strCode uniqueidentifier default NEWID(),
    strName nvarchar(MAX) NOT NULL,
        .....
        CONSTRAINT pk_configurations PRIMARY KEY (strCode)
)
CREATE TABLE tbl_environments
(
    strCode uniqueidentifier default NEWID(),
    strName nvarchar(15) NOT NULL,
    strDescription nvarchar(MAX) NOT NULL
    CONSTRAINT pk_environments PRIMARY KEY (strCode)
)
CREATE TABLE tbl_configurationsenvironments
(
    strConfiguration uniqueidentifier NOT NULL,
    strEnvironment uniqueidentifier NOT NULL
    CONSTRAINT pk_configurationsenvironments PRIMARY KEY (strConfiguration, strEnvironment),
    CONSTRAINT fk_configurationsenvironmentsconfigurations FOREIGN KEY (strConfiguration) REFERENCES tbl_configurations(strCode),
    CONSTRAINT fk_configurationsenvironmentsenvironments FOREIGN KEY (strEnvironment) REFERENCES tbl_environments(strCode)
)
 public class ConfigurationEntity
    {
        public virtual Guid strCode { get; set; }
        public virtual string strName { get; set; }
        .....
        public virtual ISet<EnvironmentEntity> colEnvironments { get; set; }
    }
public class EnvironmentEntity
    {
        public virtual Guid strCode { get; set; }
        public virtual string strName { get; set; }
        public virtual string strDescription { get; set; }
    }
public class EnvironmentMap : ClassMap<EnvironmentEntity>
    {
        public EnvironmentMap()
        {
            Table("tbl_environments");
            Id(x => x.strCode).GeneratedBy.Guid();
            Map(x => x.strName);
            Map(x => x.strDescription);
            Not.LazyLoad();
        }
    }
public class ConfigurationMap : ClassMap<ConfigurationEntity>
    {
        public ConfigurationMap()
        {
            Table("tbl_configurations");
            Id(x => x.strCode).GeneratedBy.Guid();
            Map(x => x.strName);
            Join("tbl_configurationsenvironments", m =>
            {
                m.Fetch.Join();
                m.KeyColumn("strConfiguration");
                m.HasMany<EnvironmentEntity>(x => x.colEnvironments)
                    .AsSet()
                    .Cascade.All()
                    .KeyColumn("strCode")
                    .Fetch.Join()
                    .Not.LazyLoad();
            });
           Not.LazyLoad();
        }
    }

根据我在控制台上看到的,它正确映射了 tbl_configurations 和 tbl_configurationsenvironments 之间的关系

tbl_configurations confi11_ left outer join tbl_configurationsenvironments confi11_1_ on confi11_.strCode=confi11_1_.strConfiguration

但是它没有正确设置 tbl_environments 和 tbl_configurationenvironments 之间的关系

左外连接 tbl_environments colenviron12_ on confi11_.strCode=colenviron12_.strCode

相反,它在 tbl_configurations 和 tbl_environments 之间建立了关系,我很确定错误出现在 ConfigurationMap 上,尤其是在本节中:

 m.HasMany<EnvironmentEntity>(x => x.colEnvironments)
                .AsSet()
                .Cascade.All()
                .KeyColumn("strCode")
                .Fetch.Join()
                .Not.LazyLoad();
        });

你知道我怎么能指定tbl_configurationsenvironments.strEnvironments之间的关系吗?我尝试使用 .PropertyRef("strEnvironment"),但是当我这样做时,它看起来 tbl_configurations 上的属性不正确

标签: c#fluent-nhibernate-mapping

解决方案


推荐阅读