FluentNHibernate - 映射时获取实体名称。当前方法已过时
我目前正在使用 AutoMappingOverride 自动命名我的表,并将表的名称设置为实体的复数名称,如下所示。
public class PersonMappingOverride : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Table(mapping.EntityType.Name.Pluralize());
mapping.Id(x => x.PersonId).Column("PersonId").GeneratedBy.Increment();
mapping.Map(x => x.Name).CustomSqlType("nvarchar(200)");
}
}
public static string Pluralize(this string input)
{
return System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en-US")).Pluralize(input);
}
我收到以下错误。
'FluentNHibernate.Mapping.ClasslikeMapBase.EntityType' 已过时:'不要调用此方法。实施细节被错误地公开。将在下一个版本中设为私有。
有谁知道获取实体名称的正确方法?我已经做了相当多的谷歌搜索,只找到了人们使用这种过时的方法来实现我需要的东西的例子。
干杯
史蒂夫,
感谢所有提供帮助的人。对于我的用例来说,使用约定比覆盖更优雅。
var sqlConfig = MsSqlConfiguration.MsSql2005.ConnectionString(ConnectionString);
var config = Fluently.Configure().Database(sqlConfig);
config.Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<User>(new IJAutomappingConfiguration()).Conventions.AddFromAssemblyOf<TableNamingConvention>()));
public class TableNamingConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Name.Pluralize());
}
}
这是使其正常工作的完整代码。
I am currently automatically naming my tables in fluent NHibernate by using AutoMappingOverride's and setting the name of the table to the pluralised name of the entity like so.
public class PersonMappingOverride : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Table(mapping.EntityType.Name.Pluralize());
mapping.Id(x => x.PersonId).Column("PersonId").GeneratedBy.Increment();
mapping.Map(x => x.Name).CustomSqlType("nvarchar(200)");
}
}
public static string Pluralize(this string input)
{
return System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en-US")).Pluralize(input);
}
I am getting the following error.
'FluentNHibernate.Mapping.ClasslikeMapBase.EntityType' is obsolete: 'Do not call this method. Implementation detail mistakenly made public. Will be made private in next version.'
Does anyone know the correct way of getting the entity name? I've done a fair bit of Googling and have only found examples of people using this obsolete method to achieve what I need.
Cheers
Steve
Thanks for all the great help guys. Using conventions is way more elegant for my use case than the overrides anyway.
var sqlConfig = MsSqlConfiguration.MsSql2005.ConnectionString(ConnectionString);
var config = Fluently.Configure().Database(sqlConfig);
config.Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<User>(new IJAutomappingConfiguration()).Conventions.AddFromAssemblyOf<TableNamingConvention>()));
public class TableNamingConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Name.Pluralize());
}
}
Here is the full code to get this working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是使用约定的方法:
Here's how you would do it using a Convention:
也许尝试一下约定,FluentNHibernate 约定
Maybe trying Conventions, FluentNHibernate Conventions