System.NotImplementedException when saving data in the DB

-2

I want to save the ID of the user who logged in DB in the "CreatedBy" column in the DB All identities created by the user must have the ID of the user who created them, but when clicking on the "Create" button, I receive a < strong> System.NotImplementedException , I have no idea why.

Use Identity framework with ASP.net MVC 5 and EF.

Model IdentityModel.cs , method where the error is found:

   public static implicit operator string(ApplicationUser v)
    {
        throw new NotImplementedException(); // aqui se encuentra la excepcion
    }
}

Create () control FitnessGoalController created from the FitnessGoals Model, this is where it's about saving the ID of the user logged in the DB:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Name,Goal,StartDate,FinishDate,Weight")] FitnessGoals fitnessGoals)
    {

        var user = UserManager.FindById(User.Identity.GetUserId());

        if (ModelState.IsValid)
        {

            fitnessGoals.CreatedBy = user;
            db.FitnessGoals.Add(fitnessGoals);

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(fitnessGoals);
    }
    
asked by Bryan Romero 18.08.2018 в 13:55
source

1 answer

2

This error is sent to you because you have not controlled the exceptions when saving the data in the base, your error is found by assigning the variable user the entire user entity that you are recovering with UserManager.FindById() then what you have to do is to change the line:

var user = UserManager.FindById(User.Identity.GetUserId());

by the line:

var user = User.Identity.GetUserId();

so that when assigning the value of the latter the result is the string that contains the user's Id and not the whole entity then at the time of saving it does not detect that error and it does not come out that you have not controlled the exceptions .

    
answered by 18.08.2018 / 19:53
source