Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional DbConnection to .Update #128

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public UpdateSpecification<T> ColumnsToUpdate(params Expression<Func<T, object>>
public interface IEFBatchOperationFiltered<TContext, T>
{
int Delete();
int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modifier);
int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modifier, DbConnection connection = null);
}
public static class EFBatchOperation
{
Expand Down Expand Up @@ -208,16 +208,17 @@ public int Delete()
}
}

public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modifier)
public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modifier, DbConnection connection = null)
{
var con = context.Connection as EntityConnection;
if (con == null)
if (con == null && connection == null)
{
Configuration.Log("No provider could be found because the Connection didn't implement System.Data.EntityClient.EntityConnection");
return Fallbacks.DefaultUpdate(context, this.predicate, prop, modifier);
}
var connectionToUse = connection ?? con.StoreConnection;

var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(con.StoreConnection));
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
if (provider != null && provider.CanUpdate)
{
var set = context.CreateObjectSet<T>();
Expand All @@ -241,7 +242,7 @@ public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modi
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + con.StoreConnection.GetType().Name);
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
return Fallbacks.DefaultUpdate(context, this.predicate, prop, modifier);
}
}
Expand Down
19 changes: 19 additions & 0 deletions EntityFramework.Utilities/Tests/UpdateByQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ public void UpdateAll_NoProvider_UsesDefaultDelete()
Assert.IsNotNull(fallbackText);
}

[TestMethod]
public void UpdateAll_Increment_WithExplicitConnection()
{
SetupBasePosts();

int count;
using (var db = Context.Sql())
{
count = EFBatchOperation.For(db, db.BlogPosts).Where(b => b.Title == "T2").Update(b => b.Reads, b => b.Reads + 5, db.Database.Connection);
Assert.AreEqual(1, count);
}

using (var db = Context.Sql())
{
var post = db.BlogPosts.First(p => p.Title == "T2");
Assert.AreEqual(5, post.Reads);
}
}

private static void SetupBasePosts()
{
using (var db = Context.Sql())
Expand Down