尝试使用Entity Framework保存新实体时出错

时间:2017-09-20 11:29:32

标签: c# entity-framework savechanges

我有一个VS项目,它使用Entity Framework将信息存储到数据库中。

我有这堂课:

DIET_DietHeaders

using System;
using System.Collections.Generic;

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

        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> 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<DIET_FoodsBlack>();
                this.DIET_DietHeaders = new HashSet<DIET_DietHeaders>();
            }

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

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

现在在我的控制器中,当我收到Json的请求时,我正在构建此代码以将新记录保存到DIET_DietHeaders表中。

[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));
    }
}

如果我尝试执行请求,我会收到这个奇怪的错误:

  

无法在表DIET_DietHeaders的CodiceFiscaleAssistito列中插入null

但是此字段已正确填充。

我的错误在哪里?

0 个答案:

没有答案