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 and .Delete #130

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ pip-log.txt
# Mac crap
.DS_Store
packages
.vs/
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ 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 Delete(DbConnection connection = null);
int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modifier, DbConnection connection = null);
}
public static class EFBatchOperation
{
Expand Down Expand Up @@ -181,16 +181,17 @@ public IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> pr
return this;
}

public int Delete()
public int Delete(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.DefaultDelete(context, this.predicate);
}
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.CanDelete)
{
var set = context.CreateObjectSet<T>();
Expand All @@ -203,21 +204,22 @@ public int Delete()
}
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.DefaultDelete(context, this.predicate);
}
}

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 +243,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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("EntityFramework.Utilities")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Fork of https://github.com/MikaelEliasson/EntityFramework.Utilities")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EntityFramework.Utilities")]
Expand Down
35 changes: 35 additions & 0 deletions EntityFramework.Utilities/Tests/DeleteByQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,40 @@ public void DeleteAll_NoProvider_UsesDefaultDelete()
Assert.IsNotNull(fallbackText);
}


[TestMethod]
public void DeleteAll_PropertyEquals_WithExplicitConnection_DeletesAllMatchesAndNothingElse()
{
using (var db = Context.Sql())
{
if (db.Database.Exists())
{
db.Database.Delete();
}
db.Database.Create();

db.BlogPosts.Add(BlogPost.Create("T1"));
db.BlogPosts.Add(BlogPost.Create("T2"));
db.BlogPosts.Add(BlogPost.Create("T2"));
db.BlogPosts.Add(BlogPost.Create("T3"));

db.SaveChanges();
}

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

using (var db = Context.Sql())
{
var posts = db.BlogPosts.ToList();
Assert.AreEqual(2, posts.Count);
Assert.AreEqual(0, posts.Count(p => p.Title == "T2"));
}
}

}
}
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