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

Return results from bulk inserts and updates #81

Open
wants to merge 38 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5877f5b
Merge pull request #9 from MikaelEliasson/development
MikaelEliasson Jun 27, 2014
50b00d9
Updated with 0.4 changes
MikaelEliasson Jul 8, 2014
d50e8fd
Update README.md
MikaelEliasson Aug 7, 2014
7c3f7c4
Update README.md
MikaelEliasson Aug 7, 2014
bb14ca1
Clarifying the readme regarding the DbContext
MikaelEliasson Aug 21, 2014
a897c9e
Update MappingHelper.cs
dahrnsbrak Aug 21, 2014
6c259aa
Adding another fix to get Type Hierarchies working with EFQueryHelper…
dahrnsbrak Aug 22, 2014
407f2b7
Merge pull request #11 from dahrnsbrak/patch-1
MikaelEliasson Aug 31, 2014
8dc7779
Merge pull request #15 from MikaelEliasson/development
MikaelEliasson Aug 31, 2014
4f6676e
Bumped assembly version
MikaelEliasson Aug 31, 2014
e3b453d
Fix for Include to BlogPost table
otf Sep 27, 2014
89be53d
Merge pull request #16 from otf/includefix
MikaelEliasson Oct 21, 2014
0481e72
Added license and bumped version
MikaelEliasson Oct 21, 2014
11a2892
Configurable batchsize
MikaelEliasson Oct 28, 2014
496a1a8
Merge pull request #20 from MikaelEliasson/batchsize
MikaelEliasson Oct 28, 2014
19defc6
EF version update
MikaelEliasson May 12, 2015
952c180
Updated to use new MetaData apis and fixed TPH tests
MikaelEliasson May 14, 2015
f170da0
Fix for issue #22
MikaelEliasson May 14, 2015
bae8cca
Fix for table name that's missing in DB first
MikaelEliasson May 14, 2015
bb73ad3
Fix for VB problem. Issue #29
MikaelEliasson May 14, 2015
c22ccee
Updateall intial
MikaelEliasson Aug 31, 2014
6dfaaeb
Bulk updates Issue #39
MikaelEliasson May 14, 2015
c40827c
Version number bump
MikaelEliasson May 14, 2015
f3411f6
Updated readme for new release
MikaelEliasson May 14, 2015
0c67544
Merge pull request #38 from MikaelEliasson/release10
MikaelEliasson May 14, 2015
baa5781
Update README.md
MikaelEliasson May 14, 2015
dc93f6f
Example for partial updates and info about permissions
MikaelEliasson May 15, 2015
5484f6a
Issue #47 : UpdateAll doesn't work for nvarchar(xxx)
MikaelEliasson Jun 10, 2015
a8ea308
Issue #48: Fixed UpdateAll doesn't work with decimal types
MikaelEliasson Jun 10, 2015
27df1b5
Fix for Issue #46: Replace "," with "and" in filter
MikaelEliasson Jun 10, 2015
d780b31
Fix for Issue #50: Don't include TPH discriminator in UpdateAll
MikaelEliasson Jun 10, 2015
4c26cbb
Merge pull request #49 from MikaelEliasson/release101
MikaelEliasson Jun 10, 2015
d236ecd
$(Tests,PerformanceTests)EF version update;$(EFUtilities) Fix TPHConf…
adzhiljano Jul 22, 2015
0285cda
Merge pull request #51 from adzhiljano/master
MikaelEliasson Jul 29, 2015
bad26ff
ignore tests that aren't supported right now
MikaelEliasson Jul 29, 2015
f8cd994
Merge branch 'master' of https://github.com/MikaelEliasson/EntityFram…
MikaelEliasson Jul 29, 2015
b01ec62
Version bump
MikaelEliasson Jul 29, 2015
a85a801
Initial commit for return resutls
ewassef Apr 22, 2016
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,030 changes: 1,030 additions & 0 deletions EntityFramework.Utilities/.vs/config/applicationhost.config

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions EntityFramework.Utilities/EntityFramework.Utilities.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceTests", "PerformanceTests\PerformanceTests.csproj", "{B888C7A8-D29C-4ED2-B408-C74E36808E9E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{86B2DC00-4043-4C06-8795-93BD6999B6C9}"
ProjectSection(SolutionItems) = preProject
LICENSE.txt = LICENSE.txt
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>

<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace EntityFramework.Utilities
public class ColumnMapping
{
public string NameOnObject { get; set; }
public string StaticValue { get; set; }
public string NameInDatabase { get; set; }

public string DataType { get; set; }

public bool IsPrimaryKey { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static Configuration(){
Providers.Add(new SqlQueryProvider());

Log = m => { };

DisableDefaultFallback = true;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public static void ForceDelete(this Database db, string name = null)
// if you used master db as Initial Catalog, there is no need to change database
sqlconnection.ChangeDatabase("master");

string rollbackCommand = @"ALTER DATABASE " + name + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE;";
string rollbackCommand = @"ALTER DATABASE [" + name + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;";

SqlCommand deletecommand = new SqlCommand(rollbackCommand, sqlconnection);

deletecommand.ExecuteNonQuery();

string deleteCommand = @"DROP DATABASE " + name + ";";
string deleteCommand = @"DROP DATABASE [" + name + "];";

deletecommand = new SqlCommand(deleteCommand, sqlconnection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,57 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
/// </summary>
/// <param name="items">The items to insert</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
void InsertAll(IEnumerable<T> items, DbConnection connection = null);
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;


/// <summary>
/// Bulk insert all items if the Provider supports it. Otherwise it will use the default insert unless Configuration.DisableDefaultFallback is set to true in which case it would throw an exception.
/// </summary>
/// <param name="items">The items to insert</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
IEnumerable<TEntity> InsertAllReturnInserted<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);


/// <summary>
/// Bulk update all items if the Provider supports it. Otherwise it will use the default update unless Configuration.DisableDefaultFallback is set to true in which case it would throw an exception.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="items">The items to update</param>
/// <param name="updateSpecification">Define which columns to update</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;



/// <summary>
/// Bulk update all items if the Provider supports it. Otherwise it will use the default update unless Configuration.DisableDefaultFallback is set to true in which case it would throw an exception.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="items">The items to update</param>
/// <param name="updateSpecification">Define which columns to update</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
IEnumerable<TEntity> UpdateAllReturnUpdated<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
}

public class UpdateSpecification<T>
{
/// <summary>
/// Set each column you want to update, Columns that belong to the primary key cannot be updated.
/// </summary>
/// <param name="properties"></param>
/// <returns></returns>
public UpdateSpecification<T> ColumnsToUpdate(params Expression<Func<T, object>>[] properties)
{
Properties = properties;
return this;
}

public Expression<Func<T, object>>[] Properties { get; set; }
}

public interface IEFBatchOperationFiltered<TContext, T>
Expand Down Expand Up @@ -66,17 +115,60 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
/// </summary>
/// <param name="items">The items to insert</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
public void InsertAll(IEnumerable<T> items, DbConnection connection = null)
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
{
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");
Fallbacks.DefaultInsertAll(context, items);
}

var connectionToUse = connection ?? con.StoreConnection;
var currentType = typeof(TEntity);
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
if (provider != null && provider.CanInsert)
{

var mapping = EntityFramework.Utilities.EfMappingFactory.GetMappingsForContext(this.dbContext);
var typeMapping = mapping.TypeMappings[typeof(T)];
var tableMapping = typeMapping.TableMappings.First();

var isComplext = tableMapping.PropertyMappings.Any(x => x.IsComplex);

var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName }).ToList();
if (tableMapping.TPHConfiguration != null)
{
properties.Add(new ColumnMapping
{
NameInDatabase = tableMapping.TPHConfiguration.ColumnName,
StaticValue = tableMapping.TPHConfiguration.Mappings[typeof(TEntity)]
});
}

provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize);
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
Fallbacks.DefaultInsertAll(context, items);
}
}

public IEnumerable<TEntity> InsertAllReturnInserted<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
{
Configuration.Log("No provider could be found because the Connection didn't implement System.Data.EntityClient.EntityConnection");
Fallbacks.DefaultInsertAll(context, items);
}

var connectionToUse = connection ?? con.StoreConnection;
var currentType = typeof(TEntity);
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
if (provider != null && provider.CanInsert)
{
Expand All @@ -85,15 +177,109 @@ public void InsertAll(IEnumerable<T> items, DbConnection connection = null)
var typeMapping = mapping.TypeMappings[typeof(T)];
var tableMapping = typeMapping.TableMappings.First();

var properties = tableMapping.PropertyMappings.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName }).ToList();
var complex = tableMapping.PropertyMappings.Any(x => x.IsComplex);

var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName, DataType=p.DataType,IsPrimaryKey = p.IsPrimaryKey }).ToList();
if (tableMapping.TPHConfiguration != null)
{
properties.Add(new ColumnMapping
{
NameInDatabase = tableMapping.TPHConfiguration.ColumnName,
StaticValue = tableMapping.TPHConfiguration.Mappings[typeof(TEntity)]
});
}

provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse);
return provider.InsertItemsReturnInserted(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize,dbContext);
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
Fallbacks.DefaultInsertAll(context, items);
}
return new List<TEntity>();
}


public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
{
Configuration.Log("No provider could be found because the Connection didn't implement System.Data.EntityClient.EntityConnection");
Fallbacks.DefaultInsertAll(context, items);
}

var connectionToUse = connection ?? con.StoreConnection;
var currentType = typeof(TEntity);
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
if (provider != null && provider.CanBulkUpdate)
{

var mapping = EntityFramework.Utilities.EfMappingFactory.GetMappingsForContext(this.dbContext);
var typeMapping = mapping.TypeMappings[typeof(T)];
var tableMapping = typeMapping.TableMappings.First();

var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping {
NameInDatabase = p.ColumnName,
NameOnObject = p.PropertyName,
DataType = p.DataTypeFull,
IsPrimaryKey = p.IsPrimaryKey
}).ToList();

var spec = new UpdateSpecification<TEntity>();
updateSpecification(spec);
provider.UpdateItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, spec);
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
Fallbacks.DefaultInsertAll(context, items);
}
}

public IEnumerable<TEntity> UpdateAllReturnUpdated<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecification<TEntity>> updateSpecification, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
{
Configuration.Log("No provider could be found because the Connection didn't implement System.Data.EntityClient.EntityConnection");
Fallbacks.DefaultInsertAll(context, items);
}

var connectionToUse = connection ?? con.StoreConnection;
var currentType = typeof(TEntity);
var provider = Configuration.Providers.FirstOrDefault(p => p.CanHandle(connectionToUse));
if (provider != null && provider.CanBulkUpdate)
{

var mapping = EfMappingFactory.GetMappingsForContext(this.dbContext);
var typeMapping = mapping.TypeMappings[typeof(T)];
var tableMapping = typeMapping.TableMappings.First();
var complex = tableMapping.PropertyMappings.Any(x => x.IsComplex);
var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping
{
NameInDatabase = p.ColumnName,
NameOnObject = p.PropertyName,
DataType = p.DataTypeFull,
IsPrimaryKey = p.IsPrimaryKey
}).ToList();

var spec = new UpdateSpecification<TEntity>();
updateSpecification(spec);
return provider.UpdateItemsReturnUpdated(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, spec, dbContext);
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + connectionToUse.GetType().Name);
Fallbacks.DefaultInsertAll(context, items);
return null;
}
}

public IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate)
Expand Down Expand Up @@ -166,5 +352,8 @@ public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modi
return Fallbacks.DefaultUpdate(context, this.predicate, prop, modifier);
}
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public EFDataReader(IEnumerable<T> items, IEnumerable<ColumnMapping> properties)
Properties = properties.Select(p => p.NameOnObject).ToList();
Accessors = properties.Select(p =>
{
if (p.StaticValue != null)
{
Func<T,object> func = x => p.StaticValue;
return func;
}

var parts = p.NameOnObject.Split('.');

Expand Down
Loading