Entity modify record without tracking

Viewed 44

I'm using entity framework core along with ASP.Net boiler plate.

I've generated all my entities Dtos etc which in turn is used to generate my Database tables.

An example of my entities and App services look like this:

[Table("People", Schema = "ppl")]
public class Person : Entity
{   [Required]
    public string Name { get; set; }
    [Required]
    public Status Status { get; set; }
    
    public Book Book { get; set; }
}


[Table("Books", Schema = "doc")]
public class Book : Entity
{
    [Required]
    [StringLength(260)]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    [Required]
    public BookType Type { get; set; }
}

public class PersonAppService : AsyncCrudAppService<Person, PersonDto>, IPersonAppService
{
    public new IPermissionManager PermissionManager { get; set; }
    public new IPermissionChecker PermissionChecker { get; set; }
    public new IFeatureManager FeatureManager { get; set; }
    public new IFeatureChecker FeatureChecker { get; set; }
    public new ILogger Logger { get; set; }
    public PersonAppService(IRepository<Person, int> repository) : base(repository)
    {
    }
}

What I am trying to do is add an additional person to a book without it trying to create a new book. What I do is get the first book in the table and use that in my new model as shown below.

By doing this throws an exception to say the book is already being tracked which I'm assuming is because I'm doing a GetAsync so it locks out the record?

var book = await  _bookAppService.GetAsync(new EntityDto<int>(1));


        var person = new PersonDto
        {
            Name = "Joe Blogs",
            Status = Status.GotBook,
            Book = book
        };

var person = await _personAppServiceAppService.CreateAsync(person);

Is there a way i can do this so it just adds the book id to the model without it trying to modify\update the book everytime?

Thanks all

0 Answers
Related