我正在尝试使用FluentNHibernate AutoMappings来创建一个引用其父级的实体。我能够在ClassMap<>的帮助下完成这项工作,但现在我想将所有内容移至AutoMappings。我有一个包含以下架构的表:
CREATE TABLE [dbo].[Job]
(
[Id] INT NOT NULL IDENTITY(1,1),
[ParentId] INT NULL,
PRIMARY KEY ([Id]),
CONSTRAINT [FK_Job_Parent] FOREIGN KEY ([ParentId]) REFERENCES [Job]([Id])
)
以下C#代码:
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using System;
using System.Configuration;
namespace NHibernateTest
{
public class Program
{
private static void Main(string[] args)
{
var sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings["Default"].ConnectionString))
.Mappings(m =>
{
m.AutoMappings.Add(
AutoMap.AssemblyOf<Program>(new MappingConfig()).Conventions.Setup(conv =>
{
conv.Add<DefaultReferenceConvention>();
conv.Add<DefaultHasManyConvention>();
conv.Add<SimpleForeignKeyConvention>();
}));
})
.BuildSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var tran = session.BeginTransaction())
{
var jobs = session
.QueryOver<Job>()
.List<Job>();
tran.Commit();
}
}
}
}
public class Job
{
public virtual int Id { get; set; }
public virtual Job Parent { get; set; }
}
public class MappingConfig : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type == typeof(Job);
}
}
public class SimpleForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if (property == null)
{
return type.Name + "Id";
}
return property.Name + "Id";
}
}
public class DefaultHasManyConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.Column(string.Format("{0}{1}", instance.EntityType.Name, "Id"));
instance.LazyLoad();
}
}
public class DefaultReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
var col = string.Format("{0}Id", instance.Class.Name);
instance.Column(col);
instance.LazyLoad();
}
}
}
我得到了:
创建SessionFactory时使用了无效或不完整的配置。检查PotentialReasons集合,以及InnerException以获取更多详细信息。
外键(FK7D5D63A6AE42E0BA:作业[JobId,ParentId]))必须与引用的主键具有相同的列数(作业[Id])
有没有办法只使用自动映射来执行此操作?