subsonic/SubSonic-3.0 — Issue #276: Members not populated or saved when fieldName == tableName [fix code inside]
Author: dbeiler
Created: 2011-12-23 19:08:14+00:00
State: open

I ran into an issue where a column in my database was the same name as the table itself. Subsonic correctly generated a field for it (with 'X' appended) but it wouldn't save to the datastore, and it wouldn't be populated when I fetched data from the store.

I hacked a fix in, but it's almost Christmas and I don't want to go through the official patching whatnot, so here it is (both fixes in Subsonic.Core):

in the Load method of Database.cs, below line 159, add:
I'm not sure that this issue is isolated to primary keys; I ran into it with any field that has the "X" automatically appended.

The problem lies in the fact that subsonic is using property matching to fill in the values on generated objects, but it neglects to take into account that the property may have had the "X" appended.

I'll be submitting a patch after some additional testing, but the gist of it is:

in Database.cs (beneath line 159), add an additional check to find a property which, without the appended X, matches the property we're trying to assign to:

//if we didn't find the property, maybe we'll find it without the last character, e.g. 'X'
if (currentProp == null)
{
currentProp = cachedProps.SingleOrDefault(x => x.Name.Substring(0, x.Name.Length - 1).Equals(pName, StringComparison.InvariantCultureIgnoreCase));
}

And, in DatabaseTable.cs around line 142, the "GetColumnByPropertyName" method, I added a similar check to have the right column be found:

if (this.Name.Equals(PropertyName.Substring(0, PropertyName.Length - 1)) &&
PropertyName.EndsWith("X") &&
this.Columns.Any(x => x.Name == PropertyName.Substring(0, PropertyName.Length - 1)))

Not sure this is up to snuff, but hey - it works and I get to go home. Cheers!