我在使用SubSonic 3(.0.0.3) ActiveRecord和MySQL时遇到了一个问题。
由于MySQL不允许在表名或列名中使用大写字母(或者更确切地说,如果您这样做就忽略大写字母),我决定使用下划线分隔单词,例如entity_id,然后使用CleanUp()方法添加标题大小写并删除下划线。
我的一个朋友写了一个ToTitleCase(string s)方法,看起来像这样:
string ToTitleCase(string s)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(s);
}
CleanUp()方法如下所示:
string CleanUp(string tableName){
string result=tableName;
//strip blanks
result=result.Replace(" ","");
//put your logic here...
result = ToTitleCase(result);
result = result.Replace("_", "");
return result;
}
如果我这样做了:
var entity = Entity.All().Where(e => e.EntityName.Contains("John"));
我得到了一个NotSupportedException,消息是“不支持成员'EntityName‘”。
如果我删除
result = result.Replace("_", "");
一切都很好,只是我得到的属性看起来像Entity_Id,这并不是我想要的。
如果有人知道为什么会这样,我很想听听。如果有可能修复,那就更好了!这并不是一场精彩的表演,但有点让人恼火。
发布于 2010-06-25 00:45:53
很多很多个月以来,这对我来说都是一个问题,当我在任何支持的DB上使用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));
}
接下来,您必须修改您的Structs.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将它们与已清理的属性名称进行匹配。
https://stackoverflow.com/questions/2286884
复制相似问题