0

EDIT: It was solved by setting

aUpdate.b = null;
aUpdate.c = null;

before calling update. But this does not seem like the right way to do it.

Also, currently the way I'm saving is:

Set.Update(model);
Context.SaveChanges();

EDIT II: Where I'm including the related entities:

protected override IQueryable<A> GetQueryWithIncludes(Include level)
{
  var q = Query;
  if (level == Include.Detail)
  {
      q = q.Include(x => x.c).ThenInclude(y => y.d);
      q = q.Include(x => x.b);
  }
  return q;
}

My update method:

public A Update(A update)
{
// Commented is current fix
// update.b = null;
// update.c = null;
Update(update, true);
return update;
}

public void Update(Model model, bool save)
{
  SetQuery();
  Set.Update(model); //Set is DbSet
  if (save)
  {
    Context.SaveChanges(); // Context is DBContext
  }
}

START OF ORIGINAL THREAD:

I have a Database-first application/API. The model can be simplified(there are move navigational properties) to:

public class A
{
    // some properties
    public int SomeValue {get; set;}

    // navigational properties
    public virtual B b {get; set;}
    public virtual C c {get; set;}
}

public class B
{
    // some properties

    // navigational properties
    public virtual D d {get; set;}
}

public class C
{
    //some properties
}

In my repository query to get class A, I'm including the properties B, C, D and so on.

The properties are used to create DTOs and so on.

The problem arises when trying to update an entity of class A. The database has triggers that are used for logging. When updating class A, there are appearing log entries(entities are unaltered) for changes to each of the navigational properties' tables. As an example, if I get an entity of A, and update the value of SomeValue, there will be changes to the tables for B, C and D.

The classes B, C and D in this case are never edited, and are only used to create DTOs and connecting relationships between entities.

How can I save changes to an entity of A without touching B, C, D and thus not trigger the database to make a log entry?

I have tried AsNoTracking() after the ".Include().thenInclude()..." part without success.

10
  • 2
    EF doesn't save unmodified entities. The application code is somehow telling EF that all related objects are modified, eg by using Update(A). That's only meant to attach detached objects. EF Core doesn't need this to detect that an entity was modified Commented Jan 18, 2023 at 11:54
  • 1
    Using AsNoTracking() and Include() will only make things worse. AsNoTracking() disables change tracking so EF Core no longer knows which entities were modified and has to save all of them. Include has nothing to do with change tracking, it's used to force eager loading of related entities. In fact, if the entities aren't loaded, there won't be any changes to persist Commented Jan 18, 2023 at 11:57
  • @PanagiotisKanavos Thank you, I will look into the update function immediately. To comment #2, I'm only using Include() now, the reason is to have the related entities from the query to construct DTOs Commented Jan 18, 2023 at 12:21
  • @PanagiotisKanavos Currently using Set.Update(model) on entity of A, then calling Context.SaveChanges(). What function is the correct one to use, if not update? Also, edited the post to include a solution, but I think it's not the correct way to solve the issue. Commented Jan 18, 2023 at 13:17
  • The solution is equivalent to not using Include(). If you want to update things, you don't need Include. Why is Update(model) used at all though? Where does model come from? Update tells EF that the model object and all related objects are Modified. It's not needed when model is loaded from EF itself. EF will detect any changes by itself and generate SQL to persist only the changed properties. That's what change tracking means. Commented Jan 18, 2023 at 13:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.