NHibernate Fluent添加外部程序集映射

NHibernate Fluent添加外部程序集映射,nhibernate,fluent-nhibernate,fluent-nhibernate-mapping,Nhibernate,Fluent Nhibernate,Fluent Nhibernate Mapping,我有一个项目,映射和实体存储在其他类库中,NHibernate层存储在另一个项目中。在我的测试项目中,我想通过流畅地配置添加这些映射。。。映射。。。通过可装配的而不是单独的方式。在我下面的代码中,您可以看到我只添加了一个实体但我想将其配置为扫描其他程序集。我确信我只是错过了这里显而易见的东西。。如有任何建议,将不胜感激…… [测试] public void可以生成schemafluntly() { var cfg=新配置(); Configure(); 配置=空; ISessionFactory

我有一个项目,映射和实体存储在其他类库中,NHibernate层存储在另一个项目中。在我的测试项目中,我想通过流畅地配置添加这些映射。。。映射。。。通过可装配的而不是单独的方式。在我下面的代码中,您可以看到我只添加了一个实体但我想将其配置为扫描其他程序集。我确信我只是错过了这里显而易见的东西。。如有任何建议,将不胜感激……

[测试]
public void可以生成schemafluntly()
{
var cfg=新配置();
Configure();
配置=空;
ISessionFactory SessionFactory=null;
ISession session=null;
SessionFactory=fluntly.Configure(cfg)
***要改为添加对象的“我的装配”和“自动扫描”***
.Mappings(m=>m.FluentMappings
.Add(类型(学生地图))
)
.ExposeConfiguration(x=>configuration=x)
.BuildSessionFactory();
session=SessionFactory.OpenSession();
对象id;
使用(var tx=session.BeginTransaction())
{
var result=session.Get(1541057);
tx.Commit();
Assert.AreEqual(result.StudId,1541057);
}
session.Close();
}

自动映射

如果要筛选类型,可以使用
IAutomappingConfiguration
并从
DefaultAutomappingConfiguration
派生如下:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
如果不需要筛选,也可以使用
DefaultAutomappingConfiguration
。但我的进一步示例使用了
标准配置

如下更改配置,以将类型填充到FluentNHibernate:

SessionFactory = Fluently.Configure(cfg)
    .Mappings(m => MapMyTypes(m))
    .ExposeConfiguration(x => configuration = x)
    .BuildSessionFactory();
MapMyTypes
方法应该如下所示:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
您可以添加多个程序集,并通过
StandardConfiguration
对所有程序集进行筛选

编辑

FluentMappings

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

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

您还可以像这样组合FluentMapping和AutoMapping:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
私有操作MapMyTypes()
{
返回m=>
{
MapFluent(m);
MapAuto(m);
};
}

自动映射

如果要筛选类型,可以使用
IAutomappingConfiguration
并从
DefaultAutomappingConfiguration
派生如下:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
如果不需要筛选,也可以使用
DefaultAutomappingConfiguration
。但我的进一步示例使用了
标准配置

如下更改配置,以将类型填充到FluentNHibernate:

SessionFactory = Fluently.Configure(cfg)
    .Mappings(m => MapMyTypes(m))
    .ExposeConfiguration(x => configuration = x)
    .BuildSessionFactory();
MapMyTypes
方法应该如下所示:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
您可以添加多个程序集,并通过
StandardConfiguration
对所有程序集进行筛选

编辑

FluentMappings

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

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

您还可以像这样组合FluentMapping和AutoMapping:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}
private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}
private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
私有操作MapMyTypes()
{
返回m=>
{
MapFluent(m);
MapAuto(m);
};
}

我有一个包含映射的类库。。。还有一个类库,包含我的nhibernate代码,当然还有一个测试库。在测试库中,我想扫描包含所有实体映射的类库。。看看你的例子,看起来我仍然需要提供每个实体映射。。。我只是没有看到解决办法。。抱歉:(您是否有指向您偶然引用的文档的链接?@WarrenLaFrance您只需要提供定义映射的程序集。该程序集只是使用您的一个映射类选择的。因此无需自行选择所有类。
Assembly.GetAssembly(typeof(EntityMap))
只选择程序集,而不是类。好的,这太完美了。非常感谢您提供的指针…cfg{NHibernate.cfg.Configuration}NHibernate.Cfg.Configuration-ClassMappings Count=3我有一个包含我的映射的类库…还有一个包含我的NHibernate代码的类库,当然还有一个测试库。在测试库中,我想扫描包含所有实体映射的类库。看看你的示例,我似乎仍然需要提供每个实体映射ty mapping…我只是在这里看不到解决方案..抱歉:(您是否有指向您偶然引用的文档的链接?@WarrenLaFrance您只需提供定义映射的程序集。该程序集仅由您的一个映射类选择。因此,无需自行选择所有类。
Assembly.GetAssembly(typeof(EntityMap))
只选择程序集,而不是类。好的,这太完美了。非常感谢您提供的指针…cfg{NHibernate.cfg.Configuration}NHibernate.cfg.Configuration-ClassMappings Count=3