我没有为每个有这个问题的类做一个覆盖,而是创建一个映射约定,它会截断外键上超过 30 个字符的命名方案的对象,有很多和多对多的关系:
public class OracleIHasManyConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
var keyName = string.Format(CultureInfo.InvariantCulture, "FK_{0}_{1}",
instance.Member.Name,
instance.EntityType.Name).Truncate(30);
instance.Key.ForeignKey(keyName);
}
}
public class OracleForeignKeyConvention : FluentNHibernate.Conventions.ForeignKeyConvention
{
protected override string GetKeyName(Member property, System.Type type)
{
var name = property == null
? "Id_" + type.Name.ToUnderscoredNaming()
: "Id_" + property.Name.ToUnderscoredNaming();
return name.Truncate(30);
}
}
然后我会这样称呼这些约定:
var cfg = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile("foobar.lite3"))
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Product>()
.Where(a => a.Namespace == typeof (Product).Namespace)
.Conventions.Add<OracleIHasManyConvention>()
.Conventions.Add<OracleForeignKeyConvention>()
.Conventions.Add<OracleGeneralConvention>()
.Conventions.Add<CascadeAllConvention>()
));
这是
Truncate
扩展名:
public static class StringHelper
{
public static string Truncate(this string text, int endIndex)
{
if (text.Length > endIndex)
{
text = text.Substring(0, endIndex).TrimEnd('_');
}
return text;
}
}
我希望这在任何方面都有用。:)