Cliff Cawley - 213 - Oct 28, 2014

Hi,

I’ve been trying to use UpdateOnly to update 2 columns in a database.

Something like the following:

// Get existingPerson from Db, ready to update some columns.
Person existingPerson = …;

// Update fields
existingPerson.FirstName = “John”;
existingPerson.Age = 49;

// Send just those fields back
db.UpdateOnly(existingPerson,  
    onlyFields: p => new { p.FirstName, p.Age });

However upon executing the UpdateOnly line, I get an exception:

Additional information: variable ‘p’ of type ‘DatabaseTypes.Person’ referenced from scope ‘’, but it is not defined

This is pretty much an exact copy of the example from the OrmLite page:

db.UpdateOnly(new Person { FirstName = “JJ”, Age = 12 }, 
    onlyFields: p => new { p.FirstName, p.Age });

However I can’t find the above code in the OrmLite tests anywhere and I can’t find anywhere that uses UpdateOnly to update multiple columns of an existing object (with it’s Id already populated).

What am I doing wrong?

Cliff Cawley:

I tried that exact syntax that you have in your code and I get:

A first chance exception of type ‘System.InvalidOperationException’ occurred in System.Core.dll
Additional information: variable ‘p’ of type ‘GameServer.ServiceModel.DatabaseTypes.DatabaseUser’ referenced from scope ‘’, but it is not defined

(DatabaseUser is similar to the Person class in your code)

The following single column update works:
Db.UpdateOnly(existingUser, q => q.Name, q => q.Id == existingUser.Id);

I’m using 4.0.32 from nuget, do I need a later unreleased version to get it to work?

You can try the latest version that’s on MyGet now, or wait till the next v4.0.33 version is released later today (hopefully in a few hours).

Cliff Cawley:

Thanks, I just tried 4.0.33 from MyGet and it has the same problem. I’ll wait for the next release later today and see if that works.

I’ve just added an UpdateOnly test with multiple columns in this commit: https://github.com/ServiceStack/ServiceStack.OrmLite/commit/091dbd80e1eb9818677f37b0f38f89af07ef9327

Which works as expected. So this issue may already be resolved in the latest release which you can check by trying the latest version on MyGet:
https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Otherwise if it’s still an issue can you submit an issue with a repro at:
https://github.com/ServiceStack/Issues

hmm that’s unexpected, the MyGet version is only a couple of days old which I expected to have the same behavior as master. I’m pushing a new version of MyGet now (will let you know when it’s finished deploying - takes about 15 mins).

What RDBMS are you using? I might need a small repro using DatabaseUser.

Cliff Cawley:

Cheers, I’m using MySql. If this next build doesn’t work, I’ll see about creating a small test project for you that reproduces the issue.

ok cool, MyGet just finished updating. You’ll need to clear your NuGet package cache as well: https://github.com/ServiceStack/ServiceStack/wiki/MyGet#clear-nuget-package-cache

I’ve tried the test with MySql which also works. Yeah if it’s still an issue I’d love a stand-alone repro.

Cliff Cawley:

Thanks, I don’t think I fully updated to the previous 4.0.33 properly. I realised I needed to update all my projects, not just the one.

Now that I’ve fully updated to 4.0.33, there are some other exceptions I’m getting at the moment such as:

A first chance exception of type ‘System.IndexOutOfRangeException’ occurred in MySql.Data.dll
Additional information: Could not find specified column in results: InventorySubstances

Which is complaining about the following:

[Reference]
public List<DatabaseInventorySubstance> InventorySubstances { get; set; }

This is a reference though, so it shouldn’t be looking for a column named InventorySubstances at all.

This code was working fine in 4.0.32. I’ll keep investigating and see where I end up.

ok cool, also if you can put together a small stand-alone repro I can also take a look.

Cliff Cawley:

I’m going to have to call it a night, it’s midnight here and I’m having problems reverting back to 4.0.32 and getting it to compile so that I have a base to work from. I’ll pick it up again tomorrow and build a clean repro for you.

ok thx, it looks like I’m going to be deploying today, so any updates will have to be available on MyGet and next release.

Cliff Cawley:

I’ve created a reproducible test project, boiled down from my main project. I’ve also narrowed it down much more.

The exception that is thrown is "A first chance exception of type ‘System.InvalidOperationException’ occurred in System.Core.dll"

I’d had my VS 2012 debugger set to break when the above exception was thrown, however if I don’t break on the exception, then the data is saved correctly to the database and everything appears fine.

Perhaps that’s why you haven’t reproduce it, because it’s a silent exception that doesn’t fail any unit tests?

Let me know if you want my project source and let me know where I should send it. I’d prefer not to submit it here publicly if at all possible.

Cool, yeah can you email it to demis.bellot@gmail.com

Cliff Cawley:

Cheers, you have mail :slight_smile:

Awesome thx for the repro, fixed the NRE (caused by bad refactor) with https://github.com/ServiceStack/ServiceStack.OrmLite/commit/d8f6e1bc0e8eae97cd2d6c0f148649b5a949a4d5

For removing the warnings I’m going to have to change how async support is built, I’ve added an async.cs shim so I could compile the Async API’s in .NET 4.0, but this is causing warnings because it’s a duplicate of the TaskAwaiters built into .NET 4.5. 

Anyway you can preview the latest changes that’s now on MyGet, you’ll also have to clear your NuGet package cache first:  https://github.com/ServiceStack/ServiceStack/wiki/MyGet#clear-nuget-package-cache

Cliff Cawley:

No worries! Thanks for the fast fix :slight_smile:

Cliff Cawley:

Just tested, latest 4.0.33 works great!

Hi Cliff, I’ve ended up heavily refactoring OrmLite’s async support where it’s now only available in .NET 4.5 builds which lets me remove the async.cs shim which should now remove all the warnings you were getting before. 

These changes are now available on MyGet if you want to check it out (you’ll need to clear NuGet pkg cache). 

Cliff Cawley:

Thanks Demis, the warnings are definitely fixed!

Unfortunately my unit tests now fail while trying to connect with the db.

SetUp : System.TypeLoadException : Declaration referenced in a method implementation cannot be a final method.  Type: ‘ServiceStack.OrmLite.MySql.MySqlDialectProvider’.  Assembly: ‘ServiceStack.OrmLite.MySql, Version=4.0.33.0, Culture=neutral, PublicKeyToken=null’.
   at ServiceStack.OrmLite.MySqlDialect.get_Provider()

You can reproduce it on the 4.0.33 test project I sent you previously