Wednesday, March 7, 2018

c# - Error when trying to save a new entity with Entity Framework

I have a VS project that uses Entity Framework to store the information into a database.



I have this class:



DIET_DietHeaders:



using System;
using System.Collections.Generic;


public partial class DIET_DietHeaders
{
public DIET_DietHeaders()
{
this.DIET_DietDetails = new HashSet();
}

public int ID { get; set; }
public string CodicePersoneFisiche { get; set; }
public System.DateTime Data { get; set; }

public string Name { get; set; }
public virtual ANAG_Assistiti ANAG_Assistiti { get; set; }
public virtual ICollection DIET_DietDetails { get; set; }
}


ANAG_Assistiti:



using System;
using System.Collections.Generic;


public partial class ANAG_Assistiti
{
public ANAG_Assistiti()
{
this.DIET_FoodsBlack = new HashSet();
this.DIET_DietHeaders = new HashSet();
}

public string CodicePersoneFisiche { get; set; }

public string GruppoSanguigno { get; set; }
public string FattoreRH { get; set; }

public virtual ICollection DIET_FoodsBlack { get; set; }
public virtual ANAG_PersoneFisiche ANAG_PersoneFisiche { get; set; }
public virtual ICollection DIET_DietHeaders { get; set; }
}
}



Now in my controller, I'm building this code to save the new record into the DIET_DietHeaders table when I receive a request from Json.



[Route("Diet/UpdateDiet")]
[HttpPost]
public HttpResponseMessage UpdateDiet(PatientDietDTO upa)
{
try
{
if (upa != null)
{

UserDTO u = new UserDTO(upa.UserName, upa.Password);
RMessage LoginStatus = Login(u);

if (!login)
{
return Request.CreateResponse(HttpStatusCode.OK, LoginStatus);
}
else
{
string patientA = (assistitoExistsByCF(upa.Patient) ? upa.Patient : getCFAssistoByUsername(upa.Patient));

int res = CreateDiet(upa);

if (upa.ID == null)
{
// effettua insert
var diet = new DIET_DietHeaders
{
CodicePersoneFisiche= upa.Patient
,ANAG_Assistiti = db_data.ANAG_Assistiti.First(c => c.CodicePersoneFisiche == upa.Patient)
, Data=upa.Data

,Name=upa.Name
,Calories=upa.Calories
,WaterCount=upa.WaterCount
,CaloriesTarget=upa.CaloriesTarget
,ProteinTarget=upa.ProteinTarget
,FatTarget=upa.Fat

};

diet.CodicePersoneFisiche = "CFPALUMBO22";


db_data.DIET_DietHeaders.Add(diet);
db_data.SaveChanges();

log.Debug("save done");
int id = diet.ID;
}

return Request.CreateResponse(HttpStatusCode.Created, (new RMessage((short)status_code.Success, "Diet created")));
}

}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest, (new RMessage((short)status_code.Failure, "Not well formed JSON")));
}
}
catch (Exception e)
{
e = e.GetBaseException();
log.Error(string.Format("{0} {1}", e.Message, e.StackTrace));

return Request.CreateResponse(HttpStatusCode.InternalServerError, new RMessage((short)status_code.Exception, e.Message));
}
}


If I try to execute a request I get this strange error:




Cannot to insert null into column CodiceFiscaleAssistito of table DIET_DietHeaders





but this field is correctly populated.



Where is my error?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...