1

I have the following situation. I am updating data with Entity Framework Core. This works as follows. product is a dataset of table Artikel. So there is a Artikel class in my code.

...
foreach (var product in products)
{
    var oldStatus = product.Prodstat;
    product.Prodstat = newProductStatus.Nummer;
    Changelog changelog = new Changelog(product, "PRODSTAT", oldStatus.ToString(), newProductStatus.Nummer.ToString());
    string changeLogQuery = changelog.CreateQuery;
    var parameters = changelog.GetSqlParameterList();
    var result = _Context.Database.ExecuteSqlRaw(changeLogQuery, parameters);
}
...

I am currently only updating a single field as you can see (product.Prodstat). But now I need to update more fields. I would like not to duplicate the whole code. I want to loop through the fields that need to be updated. I imagine a kind of list that I can run through, like this:

List<FieldUpdate> fieldUpdateList = new List<FieldUpdate>()
{
    new FieldUpdate()
    {
        Column = "PRODSTAT",
        OldValue = oldValue,
        NewValue = newValue,
        Field = ???
    },
    new FieldUpdate()
    {
        Column = "_DATE",
        ...
    }
}

But I don't know how to specify the field, I would like to update. In my initial example it is this part of code:

product.Prodstat = newProductStatus.Nummer;

If I want to update different fields, the field changes i.e. to

product._Date = DateTime.Now;

Is there a way to specify this product.<field> in my list? I know, I'm able to query all properties of the class by

var fields = typeof(Artikel).GetProperties().ToList();
var field = fields.Where((field) => field.Name == "Prodstat").FirstOrDefault();

But this field I cannot use for an update, I think, because I need to update not the field itself, but the value of the field on the current product.

halfer
  • 19,824
  • 17
  • 99
  • 186
dns_nx
  • 3,651
  • 4
  • 37
  • 66
  • 1
    As a side note it looks like you are journaling/auditing the changes made to the product. I did it this way: https://github.com/thepirat000/Audit.NET this might interest you. – jeb Apr 21 '23 at 10:29

0 Answers0