Fluent NHibernate AutoMapping 中的自定义命名约定
根据此 Post 可以将命名约定从“[TableName]_id”更改为“[TableName]ID”。然而,当我看到代码时,我无法使用相当新的(大约 6 周前)版本的 Fluent NHibernate 来完成此操作。
原始代码:
var cfg = new Configuration().Configure();
var persistenceModel = new PersistenceModel();
persistenceModel.addMappingsFromAssembly(Assembly.Load("Examinetics.NHibernate.Data"));
persistenceModel.Conventions.GetForeignKeyNameOfParent = type => type.Name + "ID";
persistenceModel.Configure(cfg);
factory = cfg.BuildSessionFactory();
我想出了这个:
PersistenceModel model = new PersistenceModel();
model.AddMappingsFromAssembly(assemblyType.Assembly);
model.ConventionFinder.Add(
ConventionBuilder.Reference.Always(part
=> part.ColumnName(part.EntityType.Name + part.Property.Name)));
configuration.ExposeConfiguration(model.Configure);
return configuration.BuildConfiguration();
但这给了我原始表,因为 EntityType 不是引用的实体,并且我没有看到任何属性来获取“父”实体。
我怎样才能做到这一点?
according to this Post it is possible to change the naming convention from "[TableName]_id" to "[TableName]ID". However when I saw the code, I wasn't able to do it with my rather new (about 6 weeks old) version of Fluent NHibernate.
Original code:
var cfg = new Configuration().Configure();
var persistenceModel = new PersistenceModel();
persistenceModel.addMappingsFromAssembly(Assembly.Load("Examinetics.NHibernate.Data"));
persistenceModel.Conventions.GetForeignKeyNameOfParent = type => type.Name + "ID";
persistenceModel.Configure(cfg);
factory = cfg.BuildSessionFactory();
I came up with this:
PersistenceModel model = new PersistenceModel();
model.AddMappingsFromAssembly(assemblyType.Assembly);
model.ConventionFinder.Add(
ConventionBuilder.Reference.Always(part
=> part.ColumnName(part.EntityType.Name + part.Property.Name)));
configuration.ExposeConfiguration(model.Configure);
return configuration.BuildConfiguration();
but this gives me the original Table, because EntityType is not the referenced Entity and I see no property to get the "parent" entity.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅有关使用 Fluent NHibernate Automapping 约定删除 Id 中下划线的答案。
文本的一部分、表格和类是在链接中。
解决方案:
添加一个约定,它可以是系统范围的,也可以是更受限制的。
代码示例:
现在,它将自动将
ShelfId
列映射到Product
中的Shelf
属性。See The answer about removing underscores in Ids using Fluent NHibernate Automapping conventions here.
A portion of the text, the tables and classes are in the link.
Solution:
Add a convention, it can be system wide, or more restricted.
Code example:
Now it will automap the
ShelfId
column to theShelf
property inProduct
.