R
R
Roman2018-01-05 12:39:33
ASP.NET
Roman, 2018-01-05 12:39:33

Subentity not persisting in EF Core?

An entity comes to the controller

public class IdentityUser
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Login { get; set; }
        public string PasswordHash { get; set; }
        public int RoleId { get; set; }

        public virtual List<UserDivision> UserDivisions { get; set; }
  }

    public class UserDivision
    {
        public int IdentityUserId { get; set; }
        [JsonIgnore]
        public IdentityUser IdentityUser { get; set; }

        public int DivisionId { get; set; }
        [JsonIgnore]
        public Division Division { get; set; }
    }

controller code
[HttpPut("{id}")]
        public async Task<IActionResult> PutIdentityUser([FromRoute] int id, [FromBody] IdentityUser identityUser)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != identityUser.Id)
            {
                return BadRequest();
            }
            _context.Entry(identityUser).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IdentityUserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

the UserDivisions entity is not stored in the database.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question