I got this code to update only the specific fields of an object.
var myEntity = new DBEntity { Id = 1, Field1 = true, Field2 = "Test" };
context.DBEntities.Attach(myEntity);
var entry = context.Entry(myEntity);
entry.Property(x => x.Field1).IsModified = true;
entry.Property(x => x.Field2).IsModified = true;
context.SaveChanges();
This work perfectly. Now i want to add a log entry to my entity object, each time someone makes an update i want to track it in my logs.
var myEntity = new DBEntity { Id = 1, Field1 = true, Field2 = "Test" };
var logEntry = new DBEntityLog { LogText = "Update to field1 and field2" };
myEntity.Logs.Add(logEntry);
context.DBEntities.Attach(myEntity);
var entry = context.Entry(myEntity);
entry.Property(x => x.Field1).IsModified = true;
entry.Property(x => x.Field2).IsModified = true;
// Property does not work on collection objects
context.SaveChanges();
Now how can i save the logEntry to the database as well? Right now it will still only update the fields.
Problem solved by fetching the log entry and set the EntityState to added.