亚音速3和MySQL,除去列名在清理下划线()在LINQ查询使用属性时方法导致异常
-
21-09-2019 - |
题
我使用时遇到一个问题亚音速3(.0.0.3)的ActiveRecord与MySQL。
由于MySQL不允许你在表或列名称使用大写字母(或更确切地说无视它如果这样做),我决定使用下划线词语,例如以分离ENTITY_ID,然后用清除()方法来添加标题外壳并取出下划线。结果 甲朋友写看起来像这样一个ToTitleCase(字符串s)方法:
string ToTitleCase(string s)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(s);
}
和清理()方法是这样的:
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异常,与消息“‘实体名称’不支持构件。”
如果我除去
result = result.Replace("_", "");
一切工作就好了,只有我拿到看起来像ENTITY_ID这不是我想要的东西相当的性能。
如果有谁知道为什么发生这种情况,我很乐意听到它。如果它是可以修复,甚至更好!这是没有的搅局者,但它有点恼人。
解决方案
对于许多许多个月,这是我的问题,亚音速在任何支持DB工作时,我只是避免下划线。直到昨天,当我有支持,在SQL Server数据库有下划线的传统项目。
您得SubSonic.Core的源代码内修复(文件:SubSonic.Core \架构\ 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#>
});
但问题是,一旦你清理的列名,亚音速通过匹配您的亚音速产生的属性对数据库的原始的列名的名称 查找查询中的有效列强>
这些变化将确保亚音速是匹配他们对清洁的属性名
不隶属于 StackOverflow