Entity Framework deleting object upon Update

automapper entity-framework-core


I have a problem where Entity Framework (Core) is deleting an object upon update. I think this is related to Automapper (map DTO Resource to object). I have other objects mapped the exact same way as this object and updates work just fine.

public async Task<IActionResult> UpdateFeedback(Guid Id, [FromBody] FeedbackResource feedbackResource)
    if (!ModelState.IsValid)
        return BadRequest(ModelState);
    //removing or else get a tracking error with EF
    feedbackResource.FeedbackType = null;
    var feedback = await feedbackRepository.GetFeedback(Id);

    if (feedback == null)
        return NotFound();

    //if I use this line to map, EF will delete the object upon save.  

    mapper.Map<FeedbackResource, Feedback>(feedbackResource, feedback);

    // if I map manually, i get no error
    //feedback.Title = feedbackResource.Title;
    //feedback.Details = feedbackResource.Details;
    //feedback.IsGoodFeedback = feedbackResource.IsGoodFeedback;
    //feedback.IsReviewed = feedbackResource.IsReviewed;
    //feedback.FeedbackTypeId = feedbackResource.FeedbackTypeId;

    //    feedback.ReviewDate = DateTime.Now;
    //    feedback.ReviewedBy = UserId;
    //} else {
    //    feedback.ReviewDate = null;
    //    feedback.ReviewedBy = null;

    await uow.CompleteAsync();

    return Accepted(feedback); 

I have no idea what to troubleshoot here and cannot see this issue on any google search.

12/6/2017 3:25:42 PM

Popular Answer

I was faced with a similar situation (ef core 1.1). I will assume that your problem is similar to mine.

Also a similar problem is described here

I have the following models:

1) ApplicatonUser - standard user from EF

2) AnyDAL - any class in DB, which have link to user

 public class AnyDAL
    public long Id { get; set; }

    public long UserId { get; set; }
    public ApplicationUser User { get; set; }

3) AnyDTO - model that comes from the browser side. Like your's [FromBody] FeedbackResource feedbackResource

public class AnyDTO
    public long Id { get; set; }

    public long UserId { get; set; }

    /// It is root of all evil. See below.
    /// And yes, it is bad practice.
    public ApplicationUser User { get; set; }


1) get AnyDAL from the database;

2) map AnyDTO on AnyDAL using AutoMapper _mapper.Map(DTO, DAL);

3) SaveChanges()

In one case, SaveChanges() leads to Delete, in other to Update.

What we should know: in my case property AnyDTO.User is always null after deserialization.

The choice between delete and update depends on the value of property AnyDAL.User before mapping:

1)AnyDAL.User is null - we get Update.

2)AnyDAL.User is NOT null - we get Delete.

In other words. If property AnyDAL.User changed from some value to null - entity will be deleted. Despite the fact that AnyDAL.UserId remains the same.

There is two ways to solve it:

1) Remove property User from AnyDTO;

2) Property AnyDTO.User should always has value.

1/23/2018 11:39:52 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow