MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

Computed columns should be ignored #123

Closed capesean closed 6 years ago

capesean commented 6 years ago

I have a field/column that in my EF model is:

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public decimal? CalculatedPercent { get; private set; }

When I try InsertAll I get the error The column \"CalculatedPercent\" cannot be modified because it is either a computed column or is the result of a UNION operator.

I'd think these fields should be ignored for updates/inserts?

RudeySH commented 6 years ago

If I'm not mistaken, EFUtilities only inserts in columns that have been assigned values. So, if you're getting that error, it means you are assigning a value to CalculatePercent somewhere in your code. Am I correct?

As for updates, here you explicitly pass a "list" of properties to update, so batch updates will never try to update values of columns when you don't want it to.

capesean commented 6 years ago

I ended up using db.Database.ExecuteSqlCommand with explicit columns named, excluding the calculated columns. It was for a dataload, so it improved performance too. Pretty sure I wasn't touching the calculated field, though. I'll re-open if I come across it again.