C# – Subsonic: Simple Repository – Update Crash

csimplerepositorysql-server-2008subsonic

Whenever I attempt to use a simple repository to simply update a record, I get an object reference not set to an instance of an object error. I suspect that I am doing something incorrect, can anyone see a problem with the below lines of code for saving data to subsonic?

I'm using .Net 3.5, SQL Server 2008 and Subsonic 3.0.

Thanks for any help.

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

        //Add the test object
        SimpleObject simple = new SimpleObject();
        simple.TestString = "Test";
        repo.Add(simple);

        Console.WriteLine(simple.TestString);

        //Reload the object and update it
        SimpleObject simpleReloaded = repo.Single<SimpleObject>(simple.ID);
        simpleReloaded.TestString = "Editted";
        repo.Update(simpleReloaded);//This line seems to crash repeatedly

        Console.WriteLine(repo.Single<SimpleObject>(simple.ID).TestString);

Best Answer

My guess is that for some reason you're getting a new object on the line:

SimpleObject simpleReloaded = repo.Single<SimpleObject>(simple.ID);

So when you try and update it SubSonic is throwing an exception because simpleReloaded doesn't have a primary key. Try checking the value of simple.ID and then see if simpleReloaded is actually populated or whether you're just getting a new SimpleObject

EDIT: Looks like this is a bug try pulling the latest version from github