public class PersonEntity
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual string Phone { get; set; }
public virtual string MobilePhone { get; set; }
}
public class PersonMap : ClassMapping<PersonEntity>
{
public PersonMap()
{
Schema("dbo");
Table("People");
Id(x => x.Id, map => map.Generator(Generators.Identity));
Property(x => x.FirstName, map => map.NotNullable(true));
Property(x => x.LastName, map => map.NotNullable(true));
Property(x => x.Email, map => map.NotNullable(true));
Property(x => x.Phone, map => map.NotNullable(true));
Property(x => x.MobilePhone);
}
}
这两个类都在同一个程序集中
public ISessionFactory CreateSF()
{
FluentConfiguration fCfg = Fluently.Configure().
Database( MsSqlConfiguration.MsSql2008.ConnectionString( ConfigurationManager.ConnectionStrings["eDb"].ConnectionString).ShowSql());
fCfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PersonMap>());
return fCfg.BuildSessionFactory();
}
和我的单元测试
var sf = CreateSF();
using (var _session = sf.OpenSession())
{
new PersistenceSpecification<PersonEntity>(_session)
.CheckProperty(x => x.Id, 1)
.CheckProperty(x => x.FirstName, "FName")
.CheckProperty(x => x.LastName, "LName")
.CheckProperty(x => x.MobilePhone, "12345")
.CheckProperty(x => x.Phone, "987654")
.CheckProperty(x => x.Email, "email1@email.com")
.VerifyTheMappings();
}
抛出异常
NHibernate.MappingException:没有持久性:Ers.Data.PersonEntity 一个
我正在使用Nhibernate 3.3.3。 FluentNHibernate 1.4.0.0
还有另一种修复方法吗?我已经指定了流畅的映射。
我还有其他什么吗?
答案 0 :(得分:1)
这是一个非常有趣的问题。好的...所以,你知道吗?
mapping-by-code
fluent-nhibernate
检查比较和差异NHibernate's Mapping by Code
换句话说,只需使用fluent mapping,配置就会找到它......就是它
流畅映射的一个例子
public class PersonMap : ClassMap<PersonEntity>
{
public PersonMap()
{
Schema("dbo");
Table("People");
Id(x => x.Id).GeneratedBy.Identity();
Property(x => x.FirstName).Not.Nullable();
Property(x => x.LastName).Not.Nullable();
Property(x => x.Email).Not.Nullable();
Property(x => x.Phone).Not.Nullable();
Property(x => x.MobilePhone);
}
有非常好的链接,描述mapping-by-code
,但说实话,这些是流畅映射的最佳来源 (总是在文章的底部)< / em>的
答案 1 :(得分:0)
正如Radim所说,你是通过代码映射编写的,在会话工厂的创建过程中,你只需要添加流畅的映射。
正如我在另一个主题中回答的那样,您可以通过代码或任何其他类型的映射以及Fluently
配置来使用映射。
混合不同的映射类型甚至不是问题!你不必重写它们!
对于nhibernate,映射的来源无关紧要,最后它只是在启动时加载映射,然后只是在内部使用基于映射创建的配置对象......
以下是通过Fluently
配置
.Mappings(m =>
{
foreach (var assembly in MapAssemblies)
{
m.FluentMappings.AddFromAssembly(assembly);
m.HbmMappings.AddFromAssembly(assembly);
m.AutoMappings.Add(..)
}
})
...
.ExposeConfiguration(cfg => {
foreach (var assembly in MapAssemblies)
{
cfg.AddDeserializedMapping(HbmMappingHelper.GetMappings(assembly), null);
cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly()));
}
以上使用过的助手类:
public class HbmMappingHelper
{
public static HbmMapping GetMappings(Type[] types)
{
ModelMapper mapper = new ModelMapper();
foreach (var type in types)
{
mapper.AddMappings(Assembly.GetAssembly(type).GetExportedTypes());
}
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
public static HbmMapping GetMappings(Assembly assembly)
{
ModelMapper mapper = new ModelMapper();
mapper.AddMappings(assembly.GetExportedTypes());
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}