我是NHibernate的新手,并尝试使用Fluent进行映射.我的实体类名称与必须映射到的数据库表名称不同.我使用来源于映射类ClassMap<>,但我不能指定表名:属性TableName从ClassMap是只读的.
谢谢你的帮助.
我在PostgreSQL上使用fluentnhibernate.Fluentnhibernate是最新版本.PosrgreSQL版本是8.4.我的创建ISessionFactory的代码:
public static ISessionFactory CreateSessionFactory()
{
string connectionString = ConfigurationManager.ConnectionStrings["PostgreConnectionString"].ConnectionString;
IPersistenceConfigurer config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString);
FluentConfiguration configuration = Fluently
.Configure()
.Database(config)
.Mappings(m =>
m.FluentMappings.Add(typeof(ResourceMap))
.Add(typeof(TaskMap))
.Add(typeof(PluginMap)));
var nhibConfig = configuration.BuildConfiguration();
SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig);
return configuration.BuildSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)
当我在SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig)行执行代码时; throw error:System.NotSupportedException:不支持指定的方法.请帮帮我!我非常需要解决方案.最好的祝福
有没有办法告诉NHibernate在[MyColumn]为MS SQL Server生成SQL模式导出时为所有表名和列名(如)使用方括号?我有一个遗留数据库,它使用某些列的保留名称,并且运行使用NH生成的SQL脚本会因此而抛出错误.
我想避免为每列单独指定它.
更新:我正在使用正确的方言:
MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)
Run Code Online (Sandbox Code Playgroud)
更新2:@UpTheCreek指出了正确的方向 - 反引号,这帮助我找到了" NHibernate in Action "一书中的答案(第76页):
除了引用反引号中的所有表名和列名之外,没有办法强制NHibernate在任何地方使用带引号的标识符.
我有这个
return session.Query<CoursePermission>().Where(x => x.Student.StudentId == studentId).ToList();
Run Code Online (Sandbox Code Playgroud)
我明白了
NHibernate.Exceptions.GenericADOException was unhandled by user code
Message=could not execute query
[ select courseperm0_.PermissionId as Permissi1_3_, courseperm0_.Owner as Owner3_, courseperm0_.Add as Add3_, courseperm0_.Edit as Edit3_, courseperm0_.Delete as Delete3_, courseperm0_.View as View3_, courseperm0_.StudentId as StudentId3_, courseperm0_.CourseId as CourseId3_ from CoursePermissions courseperm0_ where courseperm0_.StudentId=@p0 ]
Name:p1 - Value:757f27a2-e997-44f8-b2c2-6c0fd6ee2c2f
[SQL: select courseperm0_.PermissionId as Permissi1_3_, courseperm0_.Owner as Owner3_, courseperm0_.Add as Add3_, courseperm0_.Edit as Edit3_, courseperm0_.Delete as Delete3_, courseperm0_.View as View3_, courseperm0_.StudentId as StudentId3_, courseperm0_.CourseId as CourseId3_ from …Run Code Online (Sandbox Code Playgroud)