0

I am using WebAPI2 and i have 2 models

public class Model1
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }

    public IList<Model2> Children{ get; set; }
}

public class Model2
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }
    public int Model1Id { get; set; }

    public virtual Model1 Model1 { get; set; }
}

In my view model i am convertinh this to ViewModel using Automapper My CRUD operations of first one is fine as ViewModel also same as Model1.

For my second model (Model2), the following is my ViewModel

 public class Model2ViewModel
{
    public int Id { get; internal set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }
    public int Model1Id { get; set; }

     public string Model1Name { get; internal set; }
     public string Model1Description { get; internal set; }
}

My code like below;

    public async Task<IHttpActionResult> Post(int model1Id, Model2ViewModel model)
    {
        try
        {
            model.Model1Id= model1Id;
            var item = Mapper.Map<Model2>(model);

             myRepo.Add(item);
             myRepo.SaveAsync()
            if (!result)
            {
                return BadRequest("Could not Save to the database");
            }

            return Created(uri, Mapper.Map<Model2ViewModel>(item));
        }
        catch (ArgumentException ex)
        {
            ModelState.AddModelError(ex.ParamName, ex.Message);
            return BadRequest(ModelState);
        }
    }

I am using Repository Pattern and the logic in add record is below;

    public void Add(T entity)
    {
        entity.RecordStatus = DataStatus.Active;
        entity.CreatedDate = entity.UpdatedDate = DateTime.UtcNow;
        _context.Set<T>().Add(entity);
    }

    public async Task<bool> SaveAsync()
    {
        int count = await _context.SaveChangesAsync();
        return count > 0;
    }

When i use post method of Model2 , i got error like Name is Required of Model1. Why Model1 also trying to create. Please help me

Note: For simplicity i added my repo calling, directly in the controller code. In real code it calling business method and from there only repo called.

Akhil
  • 1,918
  • 5
  • 30
  • 74
  • Model1 is a child of Model2. If you don't want that added in the same step then either set it's EntityState to unchanged or set the nav to null. https://stackoverflow.com/questions/25441027/how-do-i-stop-entity-framework-from-trying-to-save-insert-child-objects – Steve Greene Mar 07 '18 at 19:33
  • nice i got that. Then i think issue in AutoMapper mapping. While mapping ViewModel to Model, it created Parent object. That is the issue. Any solution to avoid this? – Akhil Mar 08 '18 at 02:14
  • Yes, tell automapper to ignore it in your mapping config: `.ForMember(dest => dest.Model1, opt => opt.Ignore())` – Steve Greene Mar 08 '18 at 15:28

0 Answers0