由于表名周围的“`”,上述答案给了我一个sql server错误。它们也不包括所有类型的映射,例如子类和集合。
这对我有用:
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestingNhibernate
{
public class UnderscoreTableNameConvention : IClassConvention, IJoinedSubclassConvention, ICollectionConvention
{
public readonly string PrependToTableName = "_";
public void Apply(IClassInstance instance)
{
instance.Table(GetTableName(instance.EntityType.Name));
}
public void Apply(IJoinedSubclassInstance instance)
{
instance.Table(GetTableName(instance.EntityType.Name));
}
public void Apply(ICollectionInstance instance)
{
instance.Table(GetTableName(instance.EntityType.Name));
}
private string GetTableName(string originalName)
{
return string.Format("`{0}{1}`", PrependToTableName, originalName);
}
}
}
就像提到的@[Cole W] 一样,您需要使用以下约定构建 sessionFactory:
sessionFactory = Fluently.Configure(normalConfig)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<OrderMap>()
.Conventions.AddFromAssemblyOf<UnderscoreTableNameConvention>())
.ProxyFactoryFactory("NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate")
.BuildSessionFactory();