我是流利的nhibernate和nhibernate的新手。我想编写一个流畅的nhibernate autopersistence约定来处理为我的实体创建多对多的映射。
这就是我现在所拥有的:
using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
namespace Namespace
{
public class HasManyToManyConvention : IHasManyToManyConvention
{
public bool Accept(IManyToManyPart target) {
return true;
}
public void Apply(IManyToManyPart target) {
var parentName = target.EntityType.Name;
var childName = target.ChildType.Name;
const string tableNameFmt = "{0}To{1}";
const string keyColumnFmt = "{0}Fk";
string tableName;
if (parentName.CompareTo(childName) < 0) {
tableName = String.Format(tableNameFmt, parentName, childName);
}
else {
tableName = String.Format(tableNameFmt, childName, parentName);
}
target.WithChildKeyColumn(String.Format(keyColumnFmt, childName));
target.WithParentKeyColumn(String.Format(keyColumnFmt, parentName));
target.WithTableName(tableName);
target.Cascade.All();
}
}
}
它似乎有效,但我觉得有更好的方法可以做到这一点。
现在我的问题: