流畅的nhibernate表列类型映射
我已经使用 fluid nhibernate 几个月了,我认为它是一个非常灵活的工具(即使“工具”可能是不正确的术语):)
我必须从事一个新项目,我仍在考虑使用流畅的 nhibernate 解决方案将我的数据库映射到存储库层。问题是,由于某种原因我真的不明白,数据库不是“标准”结构的。
我的意思是表不是由外键引用的,但我确实知道在某些情况下:
表 X:列 A 类型 nvarchar TABLE Y:A 列类型 int
X 是包含数据(以代码形式)的表,而 Y 是包含 XA 描述的表。
就好像这是一个荒谬的情况!但我无法在数据库上做任何事情。 所以问题是:我可以在流畅的 nhibernate 中映射这些表,即使列具有不同类型也可以引用它们吗?
提前谢谢
i've been working with fluent nhibernate for a couple of months and i think it's a very flexible tool (even if 'tool' is maybe incorrect term) :)
i've to work on a new project and i'm still considering using fluent nhibernate solution to map my db on a repository layer. The matter is that, for some reason i really don't understand, the db is not 'standard' structured.
I mean that tables are not referenced by foreign key, but i do know that there are cases where :
TABLE X: column A type nvarchar
TABLE Y: column A type int
X is the table that contains datas (in the form of codes), while Y is the table that contains X.A description.
As if it's an absurd situation!!! but i can't do anything on the db.
So the question is: can i map these tables in fluent nhibernate, referencing them even if columns are of different types?
thx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,NHibernate 不支持具有完全不同数据类型的外键。
现在,考虑到这是一个新项目,您需要做的是修复数据库。
No, a foreign key with completely different data types is not supported by NHibernate.
Now, considering this is a new project, what you need to do is fix the database.
是的,也不是。您可以映射表,但不能映射关系。这将为您提供贫乏的域模型和存储库方法来检索相关对象,即从
GetOrdersForCustomer()
方法而不是Customer.Orders
集合访问 Orders。另一种可能性是为这些属性创建自定义用户类型,例如
IntStoredAsString
。我只使用了自定义类型的属性,所以我不确定它们作为外键的效果如何。Yes and no. You can map the tables but not the relationships. This would give you an anemic domain model and repository methods to retrieve related objects, i.e. accessing Orders from a
GetOrdersForCustomer()
method rather than aCustomer.Orders
collection.Another possibility would be to create custom user types for these properties, such as
IntStoredAsString
. I have only used custom types for properties so I'm not sure how well they work as foreign keys.