SubSonic 3 和 MySQL,在 CleanUp() 方法中从列名中删除下划线会导致在 linq-query 中使用属性时出现异常
我在使用 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 的属性,这不完全是我想要的。
如果有人知道为什么会发生这种情况,我很想听听。如果能修复就更好了!这不是什么引人注目的事情,但有点烦人。
I've run into a problem when using SubSonic 3(.0.0.3) ActiveRecord with MySQL.
Since MySQL doesn't allow you to use uppercase letters in table or column names (or rather disregards it if you do) I decided to separate words using underscores, e.g. entity_id, and then use the CleanUp() method to add title casing and remove the underscores.
A friend wrote a ToTitleCase(string s) method that looks like this:
string ToTitleCase(string s)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(s);
}
And the CleanUp() method looks like this:
string CleanUp(string tableName){
string result=tableName;
//strip blanks
result=result.Replace(" ","");
//put your logic here...
result = ToTitleCase(result);
result = result.Replace("_", "");
return result;
}
If I then do:
var entity = Entity.All().Where(e => e.EntityName.Contains("John"));
I get a NotSupportedException, with the message "The member 'EntityName' is not supported."
If I remove
result = result.Replace("_", "");
Everything works just fine, only I get properties looking like Entity_Id which is not quite what I want.
If anyone knows why this happen, I would love to hear it. If it's possible to fix, even better! It's no showstopper but it's slightly annoying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
许多个月以来,这对我来说都是一个问题,在任何受支持的数据库上使用 SubSonic 时,我只是避免使用下划线。直到昨天,我不得不支持一个在 SQL Server 数据库中带有下划线的遗留项目。
您必须在 SubSonic.Core 的源代码中修复它(文件:SubSonic.Core\Schema\DatabaseTable.cs):
找到此方法:
并将其更改为:
接下来您必须修改您的 Structs .tt:
在顶部附近找到此内容:
并添加此行:
使其变为:
问题是,一旦您清理了列名称,SubSonic 就会尝试通过匹配您的 SubSonic 生成的查询来查找查询中的有效列属性名称与数据库的原始列名称相对应。
这些更改将确保 SubSonic 将它们与清理后的属性名称相匹配。
For many many months this was an issue for me and I just avoided underscores when working with SubSonic on any supported DB. Until yesterday when I had to support a legacy project that had underscores in its SQL Server database.
You'll have to fix it within the source code of SubSonic.Core (file: SubSonic.Core\Schema\DatabaseTable.cs):
Find this method:
And change it to:
Next you'll have to modify your Structs.tt:
Find this near the top:
And add this line:
So that it becomes:
The problem is that once you cleaned up the column names, SubSonic tries the find the valid columns in your query by matching your SubSonic generated property names against the database's original column names.
These changes will make sure that SubSonic be matching them against the cleaned property name.