流畅的 NHibernate +多个数据库
我的项目需要处理三个数据库,这意味着三个会话工厂。问题是,如果我用流畅的 nhibernate 做这样的事情:
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
工厂将拾取所有映射,甚至是对应于另一个数据库的映射,
我已经看到,当使用自动映射时,你可以做这样的事情,并按名称空间过滤:
.Mappings(m => m.AutoMappings.Add(
AutoMap
.AssemblyOf<Product>()
.Where(t => t.Namespace == "Storefront.Entities")))
我还没有找到类似的流畅映射,有可能吗?我能想到的唯一解决方案是:要么为每个数据库映射类创建单独的程序集,要么将每个实体显式添加到工厂配置中。
如果可能的话,我宁愿避免两者。谢谢。
My project needs to handle three databases, that means three session factories. The thing is if i do something like this with fluent nhibernate:
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
the factories would pick up all the mappings, even the ones that correspond to another database
I've seen that when using automapping you can do something like this, and filter by namespace:
.Mappings(m => m.AutoMappings.Add(
AutoMap
.AssemblyOf<Product>()
.Where(t => t.Namespace == "Storefront.Entities")))
I havent found anything like this for fluent mappings, is it possible?? The only solutions I can think of are: either create separate assemblies for each db mapping classes or explicitly adding each of the entities to the factory configuration.
I would prefer to avoid both, if possible. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经使用 Fluent 映射文件上的(我自己的)属性来指示实体所属的数据库来实现这一点。我还有“默认”数据库的概念,并且假设没有属性的映射文件驻留在其中在默认数据库中(它可能会减少您需要装饰的类的数量)。
然后,我有初始化代码,它为每个数据库创建一个会话工厂,并为每个数据库使用反射来查找所有 ClassMap 类,检查属性以确定它属于哪个数据库,并相应地注册每个 ClassMap。
映射文件示例:
显然我已经定义了引用/使用的数据库列表。
到目前为止,我的实现是有效的,但我遇到了障碍(我希望有人可以帮忙):
我在这里问了我的问题:
如何识别特定实体的会话工厂使用 Fluent NHibernate 和多个数据库
I've implemented exactly this using (my own) Attribute on the Fluent mapping file to dictate which DB the entity belongs in. I've also got a concept of a 'default' database, and mapping files without an attribute are assumed to reside in the default DB (it may cut down on the number of classes you need to decorate).
I then have initialisation code which creates a Session Factory per database, and for each, uses reflection to find all ClassMap classes,examines the attribute to determine which DB it belongs to, and registers each ClassMap accordingly.
A mapping file example:
Obviously I've defined a list of the DBs that are referenced/used.
My implementation works so far as I've taken it, but I've hit a snag (that I hope someone can help out with):
I've asked my question here:
How to identify a particular entity's Session Factory with Fluent NHibernate and Multiple Databases
执行此操作的简单方法是将数据库名称放入架构中
然后,假设所有表都可以通过同一连接字符串访问,则您只需要一个会话工厂
The easy way of doing this is to put the database name into the Schema
Then, provided all tables are accessible on the same connection string, you only need one session factory
您还可以按类型进行过滤。这是来自“绿色字段”AutoPersistenceModel 的一行注释代码,我在与“棕色字段一”(即两个数据库)相同的程序集中使用。但我只需要过滤一种类型,因此我没有费心去拆分遗留程序集。如果每个数据库有很多它们,那么在我看来,按程序集分割它们可能是最好的选择。
如果 FNH 能够提供某种内置的多数据库支持,那就太酷了,但我不知道如何真正做到这一点;也许是某种 SessionFactories 字典,但每种情况都是如此独特。
HTH,
贝里尔
You can also filter by types. Here's a line of commented code from a "green field" AutoPersistenceModel I use in the same assembly as a "brown field one" (ie, two databases). But there is only one type I need to filter on, so I haven't bothered to split out a legacy assembly. If you have got lots of them per db then splitting them by assemblies is probably going to be best IMO.
It would be cool if FNH could provide some sort of built in multi-db support but I don't know how that really could be done; some sort of dictionary of SessionFactories maybe but every situation is just so unique.
HTH,
Berryl
虽然回复很晚,但我建议您浏览以下网址,实际上有一些解决方案需要解决,因此您可以在答案中找到解决方案。
这里是您的答案的解决方案,它是通过url 的帮助
通过实施的解决方案对我来说,到目前为止您只能实现数据库优先模型。
尝试避免我在提到的网址发布的问题中的GenerateSchema方法,因为它仍然没有实现,就像您使用该方法一样,您将能够看到创建了相同表和关系的多个数据库。
Although it is very late response, I suggest you to go through the following url which infact has some solution to be solved and hence you can find the solution in answers however.
Here is the solution to your answer which was implemented with the help of url
By the solution implemented by me you can only implement database first model as of now.
Try Avoiding GenerateSchema Method in the Question posted by me at mentioned url, because it is still not implemented as if you use that method, you will able to see the multiple databases with same tables and relationships created.