tags:

views:

223

answers:

2

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 == "[email protected]");

if (user == null)
{
    repo.Add(new User { Email = "[email protected]", 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; }
}
+3  A: 

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.

Acoustic
A: 

I ran into this problem as well and I was able to download the latest SubSonic source and the issue was already fixed. Just open the SubSonic.Core project and do a build and replace your project's reference to SubSonic.Core.

Download Latest Source http://github.com/subsonic/SubSonic-3.0

Boom - Repository Update works again!

Danny Douglass