How to update more complex objects in EFCore?

asp.net-core c# entity-framework entity-framework-core

Question

I want to update complex object property in my asp.net core 2 project.

My UserProjects class looks like:

    public class UserProject
    {
        public int UserId { get; set; }
        public User User { get; set; }
        public int ProjectId { get; set; }
        public Project Project { get; set; }
    }

My method which I want to change project status from ready to pending. With simple objects I know but with complex objects I can not achieve parameter like entry.ProjectStatus even I will use my _context.Projects. My method looks like:

        [HttpPost("changeprojectstatus/{userId}/{projectId}")]
        public IActionResult ChangeProjectStatus(int userId, int projectId)
        {
            var result = _context.UserProjects.Where(x => x.UserId == userId && x.ProjectId == 
                                                                          projectId);

            if (result != null)
            {
                // Make changes on entity
                var pr = result.Where(x => x.Project.Status == ProjectStatus.Ready);

                // Update entity in DbSet
                _context.Projects.Update(pr);
                _context.SaveChanges();
            }


            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                return Unauthorized();

            return Ok();
        }
1
0
12/18/2019 9:43:45 AM

Accepted Answer

Your error is here you are trying to update Projects using a UserProject class. In your result query you get back UserObject.

At your update you update the Projects object, which is different than your UserProjects.

I see two solutions, choose based on what you need.

First update the Projects object:

[HttpPost("changeprojectstatus/{userId}/{projectId}")]
        public IActionResult ChangeProjectStatus(int userId, int projectId)
        {
            var result = _context.UserProjects.First(x => x.UserId == userId && x.ProjectId == 
                                                                          projectId).Select(up => up.Project);

            if (result != null)
            {
                // Make changes on entity
                var pr = result.FirstOrDefault(x => x.Status == ProjectStatus.Ready);
                pr.Status = ProjectStatus.Pending; //update
                // Update entity in DbSet
                _context.Projects.Update(pr);
                _context.SaveChanges();
            }


            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                return Unauthorized();

            return Ok();
        }

Second update the UserProjects object

[HttpPost("changeprojectstatus/{userId}/{projectId}")]
        public IActionResult ChangeProjectStatus(int userId, int projectId)
        {
            var result = _context.UserProjects.First(x => x.UserId == userId && x.ProjectId == 
                                                                          projectId);

            if (result != null)
            {
                // Make changes on entity
                var pr = result.FirstOrDefault(x => x.Project.Status == ProjectStatus.Ready);
                pr.Peoject.Status = ProjectStatus.Pending; //update the status
                // Update entity in DbSet
                _context.UserProjects.Update(pr);
                _context.SaveChanges();
            }


            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                return Unauthorized();

            return Ok();
        }
1
12/18/2019 10:32:17 AM

Popular Answer

few issues:

  1. You are starting with a collection of UserProjects and not Projects
  2. in your pr variable You are still creating a collection of UserProjects, because you are running a where, and not a firstOrDefault
  3. You are trying to send a list of UserProjects as a single type of Project

First make sure PR is single, not collection. Then if UserProjects is the same as Projects you can use AutoMapper to map your USerProject to a type of Project and then update.

The big clue here was in the error message that one type cannot be another.



Related Questions





Related

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