Calling UpdateOne updates all records in the table at once.
Can someone explain to me if this is a bug in ormlite or is this normal behavior?
Regards!
using System;
using System.Data;
using System.Linq;
using NUnit.Framework;
namespace ServiceStack.OrmLite.Tests.Issues
{
public static class DbConnectionExtensions
{
/// <summary>
/// Tries to update entity with Id.
/// </summary>
public static int UpdateOne<T>(this IDbConnection connection, T entity, params string[] onlyFields)
where T : IPoco
{
return connection.UpdateOnly(entity, onlyFields, e => e.Id == entity.Id);
}
/// <summary>
/// Tries to update entity with Id.
/// </summary>
public static int UpdateOneWorkaround<T>(this IDbConnection connection, T entity, params string[] onlyFields)
where T : IPoco
{
var id = entity.Id;
return connection.UpdateOnly(entity, onlyFields, e => e.Id == id);
}
}
[TestFixture]
public class UpdateOnlyIssue : OrmLiteTestBase
{
[Test]
public void UpdateOneWhereIssue()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Poco>();
var pocos = Enumerable.Range(0, 10)
.Select(
i =>
{
var poco = new Poco()
{
Id = i,
Name = $"inserted {i}",
};
db.Insert(poco);
return poco;
})
.ToArray();
UpdateOne(1, entity => db.UpdateOnly(entity, new[] { nameof(Poco.Name), }, a => a.Id == entity.Id));
UpdateOne(3, entity => db.UpdateOneWorkaround(entity, nameof(Poco.Name)));
UpdateOne(5, entity => db.UpdateOne(entity, nameof(Poco.Name)));
void UpdateOne(int update, Func<Poco, int> updateAction)
{
pocos[update].Name = $"updated {update}";
var updated = updateAction(pocos[update]);
Assert.AreEqual(1, updated);
}
}
}
}
public interface IPoco
{
int Id { get; set; }
}
public class Poco : IPoco
{
public int Id { get; set; }
public string Name { get; set; }
}
}