1

I am using EF and have a model entity Test.cs which contains several properties and a computed property Deviation. This property should be displayed in the gridview, but no updates should happen on this property. I have defined the computed column as following:

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

In the controller method, I'll try to save the changes the following way:

public virtual ActionResult BatchUpdate(MVCxGridViewBatchUpdateValues<T, int> updateValues)
    {
        if (ModelState.IsValid)
        {
            var model = Db.Set<T>();

            // update existing records              updateValues.Update.Where(updateValues.IsValid).ForEach(updateValue => UpdateModel(model.Single(e => e.Id == updateValue.Id), null, null, excludeProperties: new[] { "Deviation" }));

            // apply changes
            Db.SaveChanges();
        }
        else
        {
            throw new Exception("Model is invalid");
        }

        return GridViewPartial();
    }

Even though I exclude the computed property Deviation, I get the following exception:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Invalid column name 'Deviation'.

Any idea what I'm doing wrong here?

Thanks in advance.

froggy
  • 51
  • 2
  • 11
  • Just itnore the property: http://stackoverflow.com/a/10572463/5246145 – 3615 May 27 '16 at 06:39
  • Unfortunately your approach doesn't fit in my case. I use the the following code to get the data from the sql server: `return Db.Database.SqlQuery("EXEC GetReportings").ToList();`. If I use the data annotation [NotMapped] for the deviation property, it is not mapped when reading the data from the stored procedure anymore. – froggy May 27 '16 at 06:56
  • Well, if you are reading it from DB, then it's not computed? As I understand computed property should be calculated somewhere in application. – 3615 May 27 '16 at 07:01
  • Then could you just add another property, mark it with NotMapped and use that for calculating the Deviation based on the column that's coming from DB? – 3615 May 27 '16 at 07:01
  • If I add the property NotMapped it works when updating a record but on the other side it doesnt map the field when reading the field from database. – froggy May 27 '16 at 20:28
  • That's why you should have two different properties: first - not mapped that is calculated; second - mapped, but not updated, used for reading the field from database. – 3615 May 28 '16 at 07:49

0 Answers0