gpt4 book ai didi

nhibernate - 按代码映射忽略 BagPropertyMapper 中的列名

转载 作者:行者123 更新时间:2023-12-03 16:49:24 24 4
gpt4 key购买 nike

我遇到了一个问题,我认为这是 NHibernate 缺陷。

我的数据库模式,包含一个简单的父子映射:

  TABLE Company
(
ID BIGINT PRIMARY KEY
)

TABLE CompanyMailRecipient
(
ID BIGINT PRIMARY KEY IDENTITY(1, 1),
Company_ID BIGINT NOT NULL FOREIGN KEY REFERENCES Company(id),
Name VARCHAR(MAX),
EmailAddress VARCHAR(MAX),
DestinationType TINYINT
)

我的类(class)。请注意 CompanyMailRecipient表有一个名为 EmailAddress 的列, 但我的 MailRecipient类有一个名为 Address 的列.
public enum MessageDestinationType
{
Normal = 1,
CC = 2,
BCC = 3
}

public class MailRecipient
{
public virtual string Name {get; set }
public virtual string Address {get; set; } // Different name to the column!
public virtual MessageDestinationType DestinationType {get; set;}
}

public class MailConfiguration
{
private Lazy<IList<MailRecipient>> _recipients = new Lazy<IList<MailRecipient>>(() => new List<MailRecipient>());
public virtual IList<MailRecipient> Recipients
{
get
{
return _recipients.Value;
}
set
{
_recipients = new Lazy<IList<MailRecipient>>(() => value);
}
}
}

public class Company
{
public virtual long Id { get; set; }
public virtual MailConfiguration MailConfiguration { get; set; }
}

映射代码
mapper.Class<Company>(
classMapper =>
{
classMapper.Table("Company");
classMapper.Component(
company => company.MailConfiguration,
componentMapper =>
{
componentMapper.Bag(mc => mc.Recipients,
bagPropertyMapper =>
{
bagPropertyMapper.Table("CompanyMailRecipient");
bagPropertyMapper.Key(mrKeyMapper =>
{
mrKeyMapper.Column("Company_Id");
});
},
r => r.Component(
mrc =>
{

mrc.Property
(
mr => mr.Name,
mrpm => mrpm.Column("Name")
);
/*****************************/
/* Here's the important bit */
/*****************************/
mrc.Property
(
mr => mr.Address,
mrpm => mrpm.Column("EmailAddress");
);
mrc.Property
(
mr => mr.DestinationType,
mrpm => mrpm.Column("DestinationType")
);
};
)
);
}
}

现在问题来了:当我尝试查询公司时,出现以下错误(重要部分以粗体显示)

NHibernate.Exceptions.GenericADOException : could not initialize a collection: [Kiosk.Server.Entities.Company.MailConfiguration.Recipients#576][SQL: SELECT recipients0_.Company_Id as Company1_0_, recipients0_.Name as Name0_, recipients0_.Address as Address0_, recipients0_.DestinationType as Destinat4_0_ FROM CompanyMailRecipient recipients0_ WHERE recipients0_.Company_Id=?] ----> System.Data.SqlClient.SqlException : Invalid column name 'Address'. at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)



但是,如果我更改我的 C# 代码,那么我的 MailRecipient类有一个名为 EmailAddress 的属性而不是 Address ,一切正常。

就像 NHibernate 忽略了我的列映射。

这是 NHibernate 错误,还是我错过了什么?

我使用的 NHibernate 版本是 4.0.4.4。

上面的例子有一个一对多的组件卡在一个卡在实体上的组件上。

我发现,如果我去掉了一层不定向,并且让我的一对多组件直接卡在实体上,那么列名就会生效。

最佳答案

是的,这确实是 NHibernate 中的一个错误。

我发布了一个修复作为拉取请求,该请求现已合并到代码库中。它应该在 4.1.1 之后的版本中。

  • Bug NH-3913
  • GitHub Commit
  • 关于nhibernate - 按代码映射忽略 BagPropertyMapper 中的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443765/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com