subsonic / SubSonic-3.0

SubSonic 3.0 for the .NET 3.5 Framework
http://subsonic.github.io/
557 stars 210 forks source link

Members not populated or saved when fieldName == tableName [fix code inside] #276

Closed ghost closed 12 years ago

ghost commented 12 years ago

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 Load 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))) {PropertyName = PropertyName.Substring(0, PropertyName.Length - 1); }

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