本文介绍了SubSonic 3和MySQL,从CleanUp()方法中的列名中删除下划线会在linq-query中使用属性时导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL上使用SubSonic 3(.0.0.3)ActiveRecord时遇到了问题.

I've run into a problem when using SubSonic 3(.0.0.3) ActiveRecord with MySQL.

由于MySQL不允许您在表名或列名中使用大写字母(或者如果您这样做,则忽略它),因此我决定使用下划线来分隔单词,例如实体ID,然后使用CleanUp()方法添加标题大写字母并删除下划线.
一位朋友写了一个ToTitleCase(string s)方法,如下所示:

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);
}

CleanUp()方法如下:

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;
}

如果我那么做:

var entity = Entity.All().Where(e => e.EntityName.Contains("John"));

我收到一个NotSupportedException,消息为不支持成员'EntityName'."

I get a NotSupportedException, with the message "The member 'EntityName' is not supported."

如果我删除

result = result.Replace("_", "");

一切正常,只有我得到的属性看起来像Entity_Id一样,并不是我想要的.

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.

推荐答案

许多年来,这对我来说一直是个问题,在SubSonic上处理任何受支持的数据库时,我只是避免使用下划线.直到昨天,我不得不支持在其SQL Server数据库中带有下划线的旧项目.

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.

您必须在SubSonic.Core(文件:SubSonic.Core \ Schema \ DatabaseTable.cs)的源代码中对其进行修复:

You'll have to fix it within the source code of SubSonic.Core (file: 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 :

Next you'll have to modify your 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生成的属性名称与数据库的原始列名称进行匹配,尝试在查询中查找有效的列.

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.

这些更改将确保SubSonic将其与清除的属性名称相匹配.

These changes will make sure that SubSonic be matching them against the cleaned property name.

这篇关于SubSonic 3和MySQL,从CleanUp()方法中的列名中删除下划线会在linq-query中使用属性时导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:03