我的域自动映射正在工作,但现在当我更新我的 NHibernate 堆栈时,我在会话工厂构建配置时遇到映射异常:
“无法弄清楚多对多属性‘Users’的另一面应该是什么。”
在多对多地图上引发异常
整个堆栈跟踪是这个:
在 FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in
c:\hornget.horn\orm\fluentnhibernate\Working-2.1\src\FluentNHibernate\Cfg\FluentConfiguration.cs:
WebApplication1.NHibernateManager.SessionFactoryManager.BuildConfiguration(AutoPersistenceModel persistenceModel
) 的第 119 行在
C:\WebProgramming\Projects\WebApplication1\WebApplication1\NHibernateManager\SessionFactoryManager.cs:第 116 行中的
WebApplication1.NHibernateManager.SessionFactoryManager.GetSessionFactory() 在
C:\WebProgramming\Projects\WebApplication1\WebApplication1\NHibernateManager\SessionFactoryManager.cs:第 71 行在
WebApplication1.NHibernateManager.SessionManager.CloseSession() 中
C:\WebProgramming\Projects\WebApplication1\WebApplication1\NHibernateManager\SessionManager.cs: C:\WebProgramming\Projects\WebApplication1\WebApplication1\Global.asax.cs中
WebApplication1.Global.Application_EndRequest(Object sender, EventArgs e) 的第 47 行: System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 处的第 36
行 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,布尔值&完成同步)
问题是如何使用自动映射正确处理多对多关系。
注意:在我更新 NHibernate/FluentNHibernate 堆栈之前,映射正在工作......
相关定义粘贴如下:
域类(在 Dll 1 中):
public class User : Entity
{
// ... removed properties
public virtual IList<Role> Roles { get; protected set; }//has many
// ... removed methods
}
public class Role : Entity
{
// ... removed properties
public virtual IList<User> Users { get; protected set; }//has many
// ... removed methods
}
实体类(在 DLL 2 中):
/// <summary>
/// Base Entity deffinition
/// </summary>
public abstract class Entity : IEquatable<Entity>
{
private int _Id = 0;
public virtual int Id { get { return _Id; } }
//... removed methods
}
约定:
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Id");
instance.GeneratedBy.HiLo("100");
instance.Access.ReadOnlyPropertyThroughPascalCaseField(PascalCasePrefix.Underscore);
}
}
public class ClassConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(Inflector.Pluralize(instance.EntityType.Name));
instance.LazyLoad();
instance.Cache.NonStrictReadWrite();
}
}
多对多约定:
public class ManyToManyConvention : IHasManyToManyConvention
{
public void Apply(IManyToManyCollectionInstance instance)
{
if (instance.OtherSide == null)
{
instance.Table(
string.Format(
"{0}To{1}",
Inflector.Pluralize(instance.EntityType.Name),
Inflector.Pluralize(instance.ChildType.Name)));
instance.Cascade.AllDeleteOrphan();
}
else
{
instance.Inverse();
}
}
}
模型:
var persistanceModel = AutoMap.AssemblyOf<DataModelPaceholder>()
.AddEntityAssembly(typeof(Entity).Assembly)
.Conventions.AddFromAssemblyOf<ClassConvention>()
.UseOverridesFromAssemblyOf<DataModelPaceholder>()
.Setup(s =>
{
s.SubclassStrategy = t => SubclassStrategy.JoinedSubclass;
s.IsComponentType = type => type == typeof(MoneyComponent);
})
.IgnoreBase<Entity>()
.IgnoreBase<EntityAudit>()
//.IncludeBase<Product>()
.Where
(
type => typeof(Entity).IsAssignableFrom(type) && !type.IsAbstract
);