Erreur Nullable object must have a value
Bonjour
Je ne sais par où commencer mais Comme le titre l'indique j'ai une erreur dont je ne comprends pas l'origine lorsque j'ajoute un personnel ,malgré maintes vérifications je n'arrive pas à comprendre où se situe l'erreur.
Citation:
Nullable object must have a value. System.InvalidOperationException at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable1.get_Value() at Fred.Business.Personnel.PersonnelManager.Add(PersonnelEnt personnel, Nullable1 userId) at Fred.Web.API.PersonnelController.<>c__DisplayClass28_0.b__0() in C:\HAMZA.Web\HAMZAWeb\API\PersonnelController.cs:line 304 at Fred.Web.API.ApiControllerBase.Post[T](Func`1 action) in C:\Hamza.Web\HamzaWEB\API\ApiControllerBase.cs:line 89
Voici la fonction de l'ajout
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public PersonnelEnt Add(PersonnelEnt personnel, int? userId = null )
{
if (personnel.IsInterimaire)
{
personnel.Matricule = GetNextMatriculeInterimaire();
}
if (userId.HasValue )
{
personnel.UtilisateurIdCreation = userId;
}
else
{
personnel.UtilisateurIdCreation = this.CurrentUser.UtilisateurId;
}
personnel.Utilisateur = null;
personnel.DateCreation = DateTime.UtcNow;
BusinessValidation(personnel);
UpdatePersonnelCategorie(personnel);
MatriculeExterneEnt matriculeToAdd = null;
if (personnel.MatriculeExterne != null && personnel.MatriculeExterne.Any())
{
matriculeToAdd = personnel.MatriculeExterne.FirstOrDefault();
personnel.MatriculeExterne = null;
}
personnel.Materiel = null;
var personnelEntCreated = this.Repository.AddPersonnel(personnel);
Save();
var personnelEnt = personnel;
if (matriculeToAdd != null )
{
matriculeToAdd.PersonnelId = personnelEnt.PersonnelId;
this.matriculeExterneManager.AddMatriculeExterne(matriculeToAdd);
}
return personnelEnt; |
Ici l'Api PersonnelController.cs
Code:
1 2 3 4 5 6
| [HttpPost]
[Route("api/Personnel")]
public HttpResponseMessage AddPersonnel(PersonnelModel model)
{
return Post(() => mapper.Map<PersonnelModel>(personnelManager.Add(mapper.Map<PersonnelEnt>(model)))); //Line304
} |
Et là la ligne 89 du ApiControllerBase.cs
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| protected HttpResponseMessage Post<T>(Func<T> action)
{
try
{
if (ModelState.IsValid)
{
var value = action(); //line 89
var response = Request.CreateResponse(HttpStatusCode.Created, value);
return response;
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState);
}
catch (ValidationException ex)
{
return this.GetValidationErrorResponse(ex);
}
catch (FredBusinessConflictException ex)
{
return GetErrorResponse(HttpStatusCode.Conflict, ex);
}
catch (FredBusinessException ex)
{
return GetErrorResponse(HttpStatusCode.BadRequest, ex);
}
catch (UnauthorizedAccessException ex)
{
return GetErrorResponse(HttpStatusCode.Forbidden, ex);
}
catch (Exception ex)
{
return GetErrorResponse(HttpStatusCode.InternalServerError, ex);
}
} |
Du coup personne n'a une idée d'où provient l'erreur? Et quelle est la solution à faire