我遇到了问题,我认为这是一个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
)
我的课程。请注意,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:无法初始化集合:[Kiosk.Server.Entities.Company.MailConfiguration.Recipients#576] [SQL:SELECT recipients0_.Company_Id as Company1_0_,recipients0_.Name as Name0_, recipients0_ .Address为Address0 _ ,recipients0_.DestinationType为Destinat4_0_ FROM CompanyMailRecipient recipients0_ WHERE recipients0_.Company_Id =?] ----&GT; System.Data.SqlClient.SqlException:列名称无效&#39;地址&#39; 。 在NHibernate.Loader.Loader.LoadCollection(ISessionImplementor会话,对象ID,IType类型)
但是,如果我更改了我的C#代码,那么我的MailRecipient
课程就会有EmailAddress
而不是Address
,那么一切正常。
就像NHibernate忽略了我的列映射一样。
这是一个NHibernate错误,还是我错过了什么?
我使用的NHibernate版本是4.0.4.4。
<小时/> 上面的例子中有一对多的组件悬挂在一个挂起实体的组件上。
我发现如果我删除了一层隐形,并让我的一对多组件直接挂在实体上,那么列名就会生效。
答案 0 :(得分:0)