按代码映射忽略 BagPropertyMapper 中的列名
Mapping-By-Code ignores Column Name in BagPropertyMapper
我遇到了一个问题,我认为这是一个 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
table 有一个名为 EmailAddress
的列,但我的 MailRecipient
class 有一个名为 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
class 有一个名为 EmailAddress
而不是 Address
的 属性,一切正常。
好像 NHibernate 忽略了我的列映射。
这是一个 NHibernate 错误,还是我遗漏了什么?
我使用的 NHibernate 版本是 4.0.4.4。
上面的例子有一个一对多的组件挂在一个挂在实体上的组件上。
我发现,如果我去掉了一个间接层,让我的一对多组件直接挂在实体上,那么列名就会生效。
是的,这确实是 NHibernate 中的一个错误。
我作为拉取请求发布了一个修复程序,现在已合并到代码库中。应该是4.1.1之后的版本。
我遇到了一个问题,我认为这是一个 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
table 有一个名为 EmailAddress
的列,但我的 MailRecipient
class 有一个名为 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
class 有一个名为 EmailAddress
而不是 Address
的 属性,一切正常。
好像 NHibernate 忽略了我的列映射。
这是一个 NHibernate 错误,还是我遗漏了什么?
我使用的 NHibernate 版本是 4.0.4.4。
上面的例子有一个一对多的组件挂在一个挂在实体上的组件上。
我发现,如果我去掉了一个间接层,让我的一对多组件直接挂在实体上,那么列名就会生效。
是的,这确实是 NHibernate 中的一个错误。
我作为拉取请求发布了一个修复程序,现在已合并到代码库中。应该是4.1.1之后的版本。