EF Core - Handling navigations when inserting a record through WebAPI

68 views Asked by At

I've got following scenario in my web application.

I start creating a new table record. Therefore I access a server-side initialization logic via WebAPI. Once the initialization on the server has completed I retrieve a instance of my entity with which I continue my work until I save it. Then I make another request to the WebAPI to call the insertion of the data. When I Add() the record do my DbContext I can see that also all navigations are added to the change tracker. This is what EF does, when call DbContext.Add().

Now I've got the problem that I cannot recognize if the value for the navigation property has been set manually with my form inputs, or if it has been already set up by my custom initialization logics. For the manual stuff I am logging all modified fields and ignore them on add() because at this point it only can be navigations to existing (already inserted) table records. So I select every single record before adding the new record. So the ChangeTracker recognizes that on Add() the navigations are existing and it does not add the navigations, due to their correct "unchanged" entity state. But this does not affect all navigation properties, which are set on server side prior. How can I deal with them.

WebAPI - Controller methods

TEntity CreateEmpty<TEntity>()

TEntity Insert(TEntity newrecord)

Server side - Model

public class Blog : TableRecord
{

    public User Admin {get;set;}

    Blog CreateEmpty()
    {
        Init();
    }

    protected override Init()
    {
        base.Init();
        Admin = CurrentUser();
    }
}

WebApp

First calls CreateNew() from API after editing the values in a form it will submit the inputs to Insert()

0

There are 0 answers