NHibernate Fluent添加外部程序集映射 [英] NHibernate Fluent Add external Assembly mappings

查看:144
本文介绍了NHibernate Fluent添加外部程序集映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我的映射和实体存储在其他类库和NHibernate层在另一个项目中。在我的测试项目中,我想通过流利的配置来添加这些映射...映射...通过安装而不是单独的。在我的代码下面,你可以看到我只添加了一个实体.. 但我想配置它来扫描我的其他程序集。我相信我只是错过了这里的明显..任何指针将不胜感激...

  public void Can_generate_schemaFluently()
{
var cfg = new Configuration();
cfg.Configure();

配置配置= null;
ISessionFactory SessionFactory = null;
ISession session = null;

SessionFactory =流利的.Configure(cfg)
***想要添加我的资产和自动扫描对象而不是***
.Mappings(m => m。 FluentMappings
.Add(typeof(StudentEOMap))

.ExposeConfiguration(x => configuration = x)
.BuildSessionFactory();

session = SessionFactory.OpenSession();

对象ID;
using(var tx = session.BeginTransaction())
{
var result = session.Get< StudentEO>(1541057);
tx.Commit();
Assert.AreEqual(result.StudId,1541057);
}
session.Close();

$ b


解决方案

AutoMapping

如果要过滤类型,可以使用 IAutomappingConfiguration DefaultAutomappingConfiguration 像这样:

  public class StandardConfiguration:DefaultAutomappingConfiguration 
{
public override bool应用映射(类型类型)
{
//实体是所有实体的基类型
返回类型(实体).IsAssignableFrom(type);




$ b $你也可以使用 DefaultAutomappingConfiguration 如果你不需要过滤。但是我的进一步的例子使用 StandardConfiguration



像这样改变你的配置,把你的类型填充到FluentNHibernate: / b>

  SessionFactory =流利.Configure(cfg)
.Mappings(m => MapMyTypes(m))
.ExposeConfiguration(x => configuration = x)
.BuildSessionFactory();

MapMyTypes 方法应该如下所示:

  private void MapMyTypes(MappingConfiguration m)
{
m.AutoMappings.Add(AutoMap.Assemblies (new StandardConfiguration(),
Assembly.GetAssembly(typeof(Entity)),
Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
);





$ b

您可以添加多个程序集,并通过 StandardConfiguration



编辑 $ b

FluentMappings



看来我误解了你的问题。要添加映射,您可以使用类似的方法来实现,但是没有 IAutomappingConfiguration
只需将 MapMyTypes 方法更改为:

  private void MapMyTypes(MappingConfiguration m)
{
m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(typeof(EntityMap)));

合并

你也可以像这样组合FluentMapping和AutoMapping: MapMyTypes()
{
return m =>
{
MapFluent(m);
MapAuto(m);
};
}


I have a project with my mappings and entities stored in other class libraries and NHibernate layers in another project. In my testing project I would like to add these mapping via fluently configure... Mappings... via assebly and not individually. In my code below you can see I added just one entity.. But I would like to configure it to scan my other assemblies. I am sure I am just missing the obvious here.. any pointers would be greatly appreciated...

 [Test]
    public void Can_generate_schemaFluently()
    {
        var cfg = new Configuration();  
        cfg.Configure();

        Configuration configuration = null;
        ISessionFactory SessionFactory = null;
        ISession session = null;

        SessionFactory = Fluently.Configure(cfg)
            *** WOULD LIKE TO ADD MY ASSEBLIES and autoscan for objects instead ***
          .Mappings(m => m.FluentMappings
                        .Add(typeof(StudentEOMap))
                  )
           .ExposeConfiguration(x => configuration = x)
            .BuildSessionFactory();

        session = SessionFactory.OpenSession();

        object id;
        using (var tx = session.BeginTransaction())
        {
            var result = session.Get<StudentEO>(1541057);
            tx.Commit();
            Assert.AreEqual(result.StudId, 1541057);
        }
        session.Close();

    }

解决方案

AutoMapping

If you want to filter through types, you can use the IAutomappingConfiguration and derive from DefaultAutomappingConfiguration like this:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}

You can also use DefaultAutomappingConfiguration if you have no need to filter. But my further example uses the StandardConfiguration.

Change your configuration like this, to populate your types to FluentNHibernate:

SessionFactory = Fluently.Configure(cfg)
    .Mappings(m => MapMyTypes(m))
    .ExposeConfiguration(x => configuration = x)
    .BuildSessionFactory();

And the MapMyTypes method should look like this:

private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}

You can add multiple Assemblies and all get filtered through the StandardConfiguration.

Edit

FluentMappings

It seems that i misread your question. To add mappings you can use a similar method to achieve that but without a IAutomappingConfiguration. Just change the MapMyTypes method to:

private void MapMyTypes(MappingConfiguration m)
{
    m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(typeof(EntityMap)));
}

Combine

You can also combine the FluentMapping and the AutoMapping like this:

private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}

这篇关于NHibernate Fluent添加外部程序集映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆