SubSonic 3 and MySQL, removing underscore from column name in CleanUp() method causes exceptions when using property in linq-query

前端 未结 1 2060
Happy的楠姐
Happy的楠姐 2021-01-16 08:46

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

相关标签:
1条回答
  • 2021-01-16 09:49

    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:

    public IColumn GetColumnByPropertyName(string PropertyName)
    {
        return Columns.SingleOrDefault(x => x.Name.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
    }
    

    And change it to:

    public IColumn GetColumnByPropertyName(string PropertyName)
    {
        return Columns.SingleOrDefault(x => x.PropertyName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase));
    }
    

    Next you'll have to modify your Structs.tt:

    Find this near the top:

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

    And add this line:

        PropertyName = "<#=col.CleanName#>",
    

    So that it becomes:

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

    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.

    0 讨论(0)
提交回复
热议问题