Column names with a hyphen throwing an exception

I’m working with a legacy SQL Server database that has a table with hyphens in the column name. OrmLite (v.4.5.4) is not building correct SQL to insert data into these columns. Here’s a quick repo:

public class DBRow
{
	[AutoIncrement]
	public int ID { get; set; }
	public string Name { get; set; }
	[Alias("Age-In-Years")]
	public int Age { get; set; }
}

var data = new[]
{
	new DBRow {Name = "John", Age = 10},
	new DBRow {Name = "Sue", Age = 12},
};
var factory = new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["db"].ConnectionString, new SqlServerOrmLiteDialectProvider());
using (var db = factory.OpenDbConnection())
{
	db.CreateTableIfNotExists<DBRow>();
	db.InsertAll(data);
}

The exception message is:

{"Incorrect syntax near '-'.\r\nMust declare the scalar variable \"@Age\"."}

Looking at the SQL generated I see:

INSERT INTO "DBRow" ("Name","Age-In-Years") VALUES (@Name,@Age-In-Years)

Is there a way to “help” OrmLite in building the variable names – an attribute or something? Or maybe tell OrmLite to use the actual property name instead of the alias as the T-SQL variable name?

I’ve just added support for customizing the Param Name used in this commit, which you can customize with:

OrmLiteConfig.ParamNameFilter = name => name.Replace("-","");

This is available from v4.5.5 that’s now available on MyGet.

Thanks Demis, but it looks like something isn’t using that filter. Using the example above, I now get:

{"Field Definition 'AgeInYears' was not found"}

Stack trace shows:

   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetParameterValues[T](IDbCommand dbCmd, Object obj)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.InsertAll[T](IDbCommand dbCmd, IEnumerable`1 objs)
   at ServiceStack.OrmLite.OrmLiteWriteApi.<>c__DisplayClass6_0`1.<InsertAll>b__0(IDbCommand dbCmd)
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec(IDbConnection dbConn, Action`1 filter)
   at ServiceStack.OrmLite.OrmLiteWriteApi.InsertAll[T](IDbConnection dbConn, IEnumerable`1 objs)

The issue was that it was using the filter but some code paths wasn’t able to perform a reverse lookup to find the field definition from the modified param name which should now be resolved in this commit.

This change is now available on MyGet, but you’ll need to clear your NuGet packages cache to pull down the latest version.

I followed the instructions to clear my nuget package cache via the command-line utility (I’m on VS2015). Deleted the packages folder and then loaded the solution. Ran the test and got the same error. Pulled up JustDecompile and I can see I don’t have the correct binaries.

So then I use nuGet to completely removed the ServiceStack references. Shut down Visual Studio, clear my package cache again and tried to delete the packages folder put it wasn’t there (makes sense since I had no more nuget packages referenced). Went back in to Visual Studio and tried to add a nuget reference to ServiceStack.OrmLite from MyGet. The only version available now is 4.5.6 but that depends on a non-existent ServiceStack.Common.v4.5.6 so I couldn’t add the reference.

Any ideas?

Apologies, my bad, I didn’t reset the version after the previous deploy. Should be resolved now. Can you try clearing your NuGet cache and downloading from MyGet again.

Works now. Thanks for the quick turnaround!

1 Like