流利的NHibernate的错误:实体“ClassMap`1”不具有标识映射 [英] Fluent NHibernate error: The entity 'ClassMap`1' doesn't have an Id mapped
问题描述
我使用到功能NHibernate正常的NHibernate的hbm.xml映射转换previous项目。目前,我卡在什么应该是最后一个步骤,以得到这个工作之一。我添加了一个派生类DefaultAutomappingConfiguration修改我的ID命名约定。字符串ID追加到类名称:
I'm converting a previous project from using normal NHibernate hbm.xml mappings to Fluent NHibernate. Currently, I'm stuck on what should be one of the last steps to getting this working. I've added a derived class for DefaultAutomappingConfiguration to modify my ID naming convention. The string "Id" is appended to the class name:
public override bool IsId(FluentNHibernate.Member member)
{
return member.Name == member.DeclaringType.Name + "Id";
}
这应该使局有一个ID在一个名为AgencyId字段。相反,我得到这个错误:
This should make "Agency" have an ID in a field named "AgencyId". Instead, I'm getting this error:
The entity 'ClassMap`1' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
{Name = "ClassMap`1" FullName = "FluentNHibernate.Mapping.ClassMap`1[[BackendDb.Model.Agency, BackendDb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
我做了一个断点上ISID功能,看看发生了什么:
I made a breakpoint on the IsId function to see what's going on:
{Property: Cache}
{Name = "ClassMap`1" FullName = "FluentNHibernate.Mapping.ClassMap`1[[BackendDb.Model.Agency, BackendDb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
这是什么?对象是不是我已经创建。每隔对象通过这个功能很好,和那些我其实是想映射正在返回正确的值。
What is this? The object is not something I've created. Every other object passes through this function fine, and the ones I actually wanted to map are returning the proper value.
我的会话工厂看起来是这样的:
My Session factory looks something like this:
var cfg = new MapConfig();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(m => m.Server(@".\SqlExpress")
.Database("{some dbname}")
.TrustedConnection()))
.Mappings(m =>
m.AutoMappings
.Add(AutoMap.AssemblyOf<Agency>(cfg))
)
.BuildSessionFactory();
烦人,似乎这在某种程度上造成了三桌,我在测试功能NHibernate在我的开发数据库是清空。什么玩意?
推荐答案
SessionFactory则是试图自动映射中的所有类,其中包含基于该指令的代理
类的程序集:添加(AutoMap.AssemblyOf&LT;机构&GT;(CFG))
。既然你有一个 AgencyMap
在装配和 ClassMap&LT;&GT;
不具有标识
属性,FNH是抛出一个错误。
The sessionfactory is trying to automap all classes in the assembly that contains your Agency
class based on this directive: Add(AutoMap.AssemblyOf<Agency>(cfg))
. Since you have an AgencyMap
in the assembly andClassMap<>
does not have an Id
property, FNH is throwing an error.
如果你想使用 ClassMap&LT;&GT;
配置,而不是(或补充)宣布的自动映射配置,声明流利的映射:
If you want to use ClassMap<>
configurations, instead of (or in addition to) declaring an automapping configuration, declare a fluent mapping:
m.FluentMappings.AddFromAssemblyOf<Agency>();
如果您不需要AutoMappings,删除`.AutoMappings.Add'指令。
If you don't need AutoMappings, remove the `.AutoMappings.Add' directive.
不过,如果你想使用AutoMappings,你需要告诉FNH你想要什么类映射。为了解决这个问题,我通常会定义一个标记接口:
However, if you want to use AutoMappings, you need to tell FNH what classes you want to map. To handle this, I usually define a marker interface:
public abstract class Entity : IPersistable
{
public virtual int Id { get; set; }
}
public interface IPersistable
{
}
然后,我从 DefaultAutomappingConfiguration
派生类的,我告诉FNH只图有该接口的类(你可以限制映射类,但是你认为合适的):
Then, in the class that I derive from DefaultAutomappingConfiguration
, I tell FNH to only map the classes that have that interface (you can limit the mapped classes however you see fit):
public class EntityAutoMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.GetInterfaces().Contains(typeof (IPersistable));
}
}
要处理的主键映射,我创建了一个约定类:
To handle the primary key mapping, I create a convention class:
public class PrimaryKeyNamePlusId : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name+"Id");
}
}
最后,配置我的SessionFactory使用的配置/会展类:
Finally, I configure my SessionFactory to use the configuration/convention classes:
m.AutoMappings.AssemblyOf<Entity>(new EntityAutoMappingConfiguration())
.IgnoreBase<Entity>()
.UseOverridesFromAssemblyOf<Entity>()
.Conventions.AddFromAssemblyOf<Entity>();
这篇关于流利的NHibernate的错误:实体“ClassMap`1”不具有标识映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!