SubSonic SimpleRepository Updates Cause Null Reference Exceptions

subsonic

In researching the SubSonic's new SimpleRepository, I've found that calling the Update() method always throws a NullReferenceException. This is even true in the sample MVC download that's included with the 3.0.0.3 release.

Does anyone know if there's a way to get updates to succeed?

Here's an example. The if statement works; it adds the table and creates the record. Running this code a second time flow to the else block, and the update throws the exception.

var repo = new SimpleRepository("c", SimpleRepositoryOptions.RunMigrations);

var user = repo.Single<User>(u => u.Email == "a@b.com");

if (user == null)
{
    repo.Add(new User { Email = "a@b.com", Name = "Test" });
}
else
{
    user.Name = DateTime.Now.ToString();
    repo.Update(user);
}

public class User
{
    public int Key { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

Best Answer

I think I found the problem. In the SubSonic source there's a minor flaw in the Update routine where it queries the list of tables in the update query object for a column name. The Linq query needed to use the column's QualifiedName property, not the Name property. The query settings (which is the right hand side of the query) uses the fully qualified name.

I took the liberty of submitting an issue on SubSonic's GitHub site as well :)

For those interested, the issue is in Update.cs (in the Query folder), Line 229.

Change this...

var col= table.Columns.SingleOrDefault(
  x => x.Name.Equals(s.ColumnName, StringComparison.InvariantCultureIgnoreCase)
);

to this...

var col = table.Columns.SingleOrDefault(
  x => x.QualifiedName.Equals(
    s.ColumnName, StringComparison.InvariantCultureIgnoreCase
  )
);

Rebuild and you're good to go.