NHibernate:将多个列映射到一个集合中
假设我有一个表:
ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------
和 .NET 类,
class A {
int id;
List<MyEmail> emails;
}
class MyEmail {
string email;
}
我想没有办法将这些(多个)列映射到 NHibernate 中的单个集合中,或者有吗? :)
到了这样的地步,我们不想再修改数据库模式了,所以我们不能对数据库做太多事情,如果这有帮助的话。
Suppose I have a table:
ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------
and the .NET classes
class A {
int id;
List<MyEmail> emails;
}
class MyEmail {
string email;
}
I suppose there's no way to map those (multiple) columns into a single collection in NHibernate, or is there? :)
It's come to a point that we'd rather not tinker with the database schema anymore so we can't do much with the database, if that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用接口,这样你就可以做这样的事情
你的应用程序可以期待一个 IUser,而不关心我们从哪里获得电子邮件列表。您可以在 NH 中映射 MyUser,而应用程序不(也不应该)关心实际的实现。
I would suggest working with Interfaces so you could do something like this
Your application can expect an IUser and not care where we got the list of emails. You would map MyUser in NH, while the application does not (and should not) care about the actual implementation.
如果它不必是集合,但可以是自定义类型,请说包含三个属性的
EmailAddresses
:您可以使用组件将三列映射到该对象的三个属性作为父级上的单个属性:
您可以使用 组件 或者如果您使用 Fluent NHibernate 和
ComponentMap
类映射(automapper 不能做组件)。If it doesn't have to be a collection, but could be a custom type instead, say
EmailAddresses
which contains three properties:You could use a component to map the three columns into the three properties of this object as a single property on the parent:
You can map these in NHibernate using components or if you're using Fluent NHibernate with the
ComponentMap<T>
classmap (automapper can't do components).有一个功能非常接近您想要的功能,
文档位于 http://nhibernate.info/doc/nh/en/index.html#components-dynamic 应该可以帮助您入门。
There is a feature that's very close to what you want,
<dynamic-component>
The documentation at http://nhibernate.info/doc/nh/en/index.html#components-dynamic should get you started.