流利的NHibernate +多个数据库多个、流利、数据库、NHibernate

2023-09-03 13:05:26 作者:世界与我无关

我的项目需要处理三个数据库,这意味着三届工厂。事情是,如果我做这样的事情用流利的NHibernate:

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.

我会preFER避免两个,如果可能的话。谢谢你。

I would prefer to avoid both, if possible. Thanks.

推荐答案

我已经完全实现了这个使用(我自己)在流利的映射文件属性,以决定哪些数据块中的实体属于,我也已经有了一个概念一个默认的数据库,并没有一个属性映射文件被假定为位于默认的数据库(它可能会削减你需要装饰类的数量)。 然后,我有初始化code的创建每个数据库会话工厂,并为每个,使用反射来找到所有ClassMap类,检查属性,以确定哪些DB它属于,并记录每个ClassMap相应。

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.

一个映射文件,例如:

  [FluentNHibernateDatabase("MySecurityDatabase")]
  public class SystemUserMap : ClassMap<SystemUser>
  {
    public SystemUserMap()
    {
      Id(x => x.SystemUserId);
      Map(x => x.LoginId);
      Map(x => x.LoginPassword);
      Map(x => x.UserFirstName);
      Map(x => x.UserSurname);
      References(x => x.UserOrganisation, "OrganisationId");
    }
  }

很显然,我已经定义了被引用/使用的数据块列表。 我的执行工作,到目前为止,我已经采取了,但我已经遇到了障碍(即我希望有人可以帮忙带):

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):

我问过我的问题在这里: http://stackoverflow.com/questions/2698503/how-to-identify-a-particular-entitys-session-factory-with-fluent-nhibernate-and

I've asked my question here: http://stackoverflow.com/questions/2698503/how-to-identify-a-particular-entitys-session-factory-with-fluent-nhibernate-and