Fluent NHibernate 有很多很多约定问题
我是流利的 nhibernate 和 nhibernate 的新手。 我想编写一个流畅的 nhibernate 自动持久约定来处理为我的实体创建多对多映射。
这就是我现在所拥有的:
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();
}
}
}
它似乎有效,但我觉得有更好的方法可以做到这一点。
现在我的问题是:
- 你有更好的方法吗?
- 您通常想要这里的级联行为吗?
- 除了确保该关联的双方最终具有相同的表名之外,我还需要担心其他事情吗?
I am new to fluent nhibernate and nhibernate. I want to write a fluent nhibernate autopersistence convention to handle creating the many to many mappings for my entities.
This is what I have right now:
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();
}
}
}
It seems to work, but I feel that there is a better way to do this.
Now my questions:
- Do you have a better way to do this?
- Do you usually want the Cascade behavior here?
- Do I need to worry about something besides making sure both sides of this association end up with the same table name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论