亚音速3和MySQL,在CleanUp()方法中从列名中删除下划线会导致在linq查询中使用属性时出现异常

亚音速3和MySQL,在CleanUp()方法中从列名中删除下划线会导致在linq查询中使用属性时出现异常,mysql,subsonic,subsonic3,subsonic-active-record,Mysql,Subsonic,Subsonic3,Subsonic Active Record,我在MySQL中使用亚音速3(.0.0.3)ActiveRecord时遇到了一个问题 由于MySQL不允许您在表名或列名中使用大写字母(或者如果您使用,则忽略它),我决定使用下划线分隔单词,例如entity_id,然后使用CleanUp()方法添加标题大小写并删除下划线。 一位朋友编写了一个ToTitleCase(string s)方法,如下所示: string ToTitleCase(string s) { CultureInfo cultureInfo = Thread.Curren

我在MySQL中使用亚音速3(.0.0.3)ActiveRecord时遇到了一个问题

由于MySQL不允许您在表名或列名中使用大写字母(或者如果您使用,则忽略它),我决定使用下划线分隔单词,例如entity_id,然后使用CleanUp()方法添加标题大小写并删除下划线。
一位朋友编写了一个ToTitleCase(string 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;
}
CleanUp()方法如下所示:

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,消息是“不支持成员'EntityName'

如果我删除

result = result.Replace("_", "");
一切都很好,只是我得到的属性看起来像实体_Id,这不是我想要的


如果有人知道为什么会这样,我很想听听。如果可以修复,那就更好了!这不是一个好问题,但有点烦人。

在很多个月的时间里,这对我来说都是一个问题,在任何受支持的DB上使用亚音速时,我都避免使用下划线。直到昨天,我还不得不支持一个在SQLServer数据库中有下划线的遗留项目

您必须在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#>
});
Columns.Add(新数据库列(“,this))
{
IsPrimaryKey=,
数据类型=数据库类型。,
IsNullable=,
自动增量=,
IsForeignKey=,
最大长度=
});
并添加以下行:

    PropertyName = "<#=col.CleanName#>",
PropertyName=“”,
使其成为:

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#>
});
Columns.Add(新数据库列(“,this))
{
PropertyName=“”,
IsPrimaryKey=,
数据类型=数据库类型。,
IsNullable=,
自动增量=,
IsForeignKey=,
最大长度=
});
问题是,一旦清理了列名,亚音速将通过将亚音速生成的属性名与数据库的原始列名相匹配,尝试在查询中查找有效列


这些更改将确保亚音速将它们与清理后的属性名称相匹配

您所使用的版本我对大写和小写没有任何问题,您将发现使用mysql和亚音速时,自动生成的键需要是TableNameID减去S,并且表名也以s结尾,以便在不存在时创建表
HomeScrolls
HomeScrollID
mediumint(9)NOT NULL自动递增,
HomeScrollImage
varchar(255)NOT NULL,
HomeScrollShow
tinyint(1)NOT NULL,主键(
HomeScrollID
),KEY
HomeScrollShow
HomeScrollShow
)ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;大写字母很好用!!!!!!!!我正在使用MySQL 5.1.36社区。我可以用大写创建表和列,但正如我所说,MySQL忽略了这一点,当我运行亚音速模板时,属性是用小写名称创建的。