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

How to use Bulk Update for all columns? #106

Open
KiranSaj opened this issue Feb 9, 2017 · 4 comments
Open

How to use Bulk Update for all columns? #106

KiranSaj opened this issue Feb 9, 2017 · 4 comments

Comments

@KiranSaj
Copy link

KiranSaj commented Feb 9, 2017

I have created following method to BulkUpdate. I am passing list to it. How to update all the column.
I want to make it generic method.

public virtual void BulkUpdate(IEnumerable entities)
{
if (entities == null)
throw new ArgumentNullException(nameof(entities));

        EFBatchOperation.For(_Context, _Entities).UpdateAll(entities,x => x.ColumnsToUpdate(c => c));
    }
@KiranSaj KiranSaj changed the title How to use Bulk Update to all columns? How to use Bulk Update for all columns? Feb 9, 2017
@CodyMorris
Copy link

Curious about this too

@RudeySH
Copy link

RudeySH commented Feb 8, 2018

You can solve this using reflection. I have done it before, you have to build the updateSpecification parameter manually using reflection. Keep in mind that when doing so, you have to ignore certain properties, such as primary keys, computed columns, navigation properties, NotMapped properties, etc.

@TaoGodNo1
Copy link

TaoGodNo1 commented Jun 13, 2019

You can solve this using reflection. I have done it before, you have to build the updateSpecification parameter manually using reflection. Keep in mind that when doing so, you have to ignore certain properties, such as primary keys, computed columns, navigation properties, NotMapped properties, etc.
I using reflection and expression tree to write this function:

EFBatchOperation.For(_Context, _Entities).UpdateAll(entities,x => x.ColumnsToUpdate(CreateExpressionFromEntity<yourEntityType>()));





public static Expression<Func<TEntity, object>>[] CreateExpressionFromEntity<TEntity>()
        {
            var listExpr = new List<Expression<Func<TEntity, object>>>();
            foreach (var item in typeof(TEntity).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(p => p.CanWrite))
            {
                var par = Expression.Parameter(typeof(TEntity));
                var pro = Expression.Property(par, item.Name);
                var conv = Expression.Convert(pro, typeof(object));
                listExpr.Add(Expression.Lambda<Func<TEntity, object>>(conv, par));
            }
            return listExpr.ToArray();
        }

@RudeySH
Copy link

RudeySH commented Jun 13, 2019

@SuperGodTao that's a great start. I suggest adding the following:

// skip primary key properties
if (Attribute.IsDefined(item, typeof(KeyAttribute)))
{
	continue;
}

// skip generated properties
if (Attribute.IsDefined(item, typeof(DatabaseGeneratedAttribute)))
{
	continue;
}

// skip not mapped properties
if (Attribute.IsDefined(item, typeof(NotMappedAttribute)))
{
	continue;
}

// skip collection properties
if (item.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
	continue;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants