许多个月以来,这对我来说都是一个问题,在任何受支持的数据库上使用 SubSonic 时,我只是避免使用下划线。直到昨天,我不得不支持一个在 SQL Server 数据库中带有下划线的遗留项目。
您必须在 SubSonic.Core 的源代码中修复它(文件:SubSonic.Core\Schema\DatabaseTable.cs):
找到这个方法:
public IColumn GetColumnByPropertyName(string PropertyName)
{
return Columns.SingleOrDefault(x => x.Name.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
}
并将其更改为:
public IColumn GetColumnByPropertyName(string PropertyName)
{
return Columns.SingleOrDefault(x => x.PropertyName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
}
接下来你必须修改你的结构.tt:
在顶部附近找到这个:
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
DataType = DbType.<#=col.DbType.ToString()#>,
IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>,
MaxLength = <#=col.MaxLength#>
});
并添加这一行:
PropertyName = "<#=col.CleanName#>",
从而变成:
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
PropertyName = "<#=col.CleanName#>",
IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
DataType = DbType.<#=col.DbType.ToString()#>,
IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>,
MaxLength = <#=col.MaxLength#>
});
问题是,一旦您清理了列名,SubSonic 就会尝试通过匹配 SubSonic 生成的查询来查找查询中的有效列属性名称与数据库的原始数据相对应列名.
这些更改将确保 SubSonic 将它们与已清理的内容相匹配属性名称.