【问题标题】:DBUpdateException while saving entities保存实体时出现 DBUpdateException
【发布时间】:2014-02-19 14:39:39
【问题描述】:

当我在 TratamientoRealizadoPSIG 上创建新的数据库注册时,SaveChanges() 函数会抛出此异常:

保存不为其关系公开外键属性的实体时发生错误。 EntityEntries 属性将返回 null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地处理保存时的异常。有关详细信息,请参阅 InnerException。

看了一下,找不到错误。

这是实体代码:

public partial class TratamientoRealizadoPSIG
{
    [Key, Column(Order = 1)]
    public Int32 Id { get; set; }

    [Required]
    public DateTime FechaRealizacion { get; set; }
    [Required]
    public TimeSpan HoraRealizacion { get; set; }
    public DateTime? FechaRegistro { get; set; }
    public String Comentario { get; set; }
    public String UsuarioRegistro { get; set; }
    public virtual String Nombre { get; set; }
    public virtual String Motivo { get; set; }
    public virtual String Dinamica { get; set; }
    public virtual Boolean Asiste { get; set; }

    [Required]
    [InverseProperty("TratamientoRealizadoPSIG")]
    public virtual Equipo Equipo { get; set; }

    [Required]
    [InverseProperty("TratamientoRealizadoPSIG")]
    public virtual Demanda Demanda { get; set; }

    [Required]
    [InverseProperty("TratamientoRealizadoPSIG")]
    public virtual PoblacionObjetivo PoblacionObjetivo { get; set; }

    [Required]
    [InverseProperty("TratamientoRealizadoPSIG")]
    public virtual ObjetivosGrupales ObjetivosGrupales { get; set; }

这是视图模型:

public partial class CreateTratamientoRealizadoPSIGViewModel
{

    // Entity properties
    public Int32 Id { get; set; }
    [Required]
    public DateTime FechaRealizacion { get; set; }
    [Required]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh\\:mm\\:ss}")]
    public TimeSpan HoraRealizacion { get; set; }
    public DateTime? FechaRegistro { get; set; }
    public String Comentario { get; set; }
    public String UsuarioRegistro { get; set; }
    public String Nombre { get; set; }
    public String Demanda { get; set; }
    public String Motivo { get; set; }
    public String ObjetivosGrupales { get; set; }
    public String PoblacionObjetivo { get; set; }
    public String Dinamica { get; set; }
    public Boolean Asiste { get; set; }

    [Required]
    [Display(Name = "Demanda")]
    public int? DemandaId { get; set; }
    [Required]
    [Display(Name = "ObjetivosGrupales")]
    public int? ObjetivosGrupalesId { get; set; }
    [Required]
    [Display(Name = "PoblacionObjetivo")]
    public int? PoblacionObjetivoId { get; set; }
    /*
    public int? ResultadoTestId { get; set; }
    [Required]
    [Display(Name="TipoDeTest")]
    public int? TipoDeTestId { get; set; }
     * */
    [Required]
    [Display(Name="Equipo")]
    public int? EquipoId { get; set; }
    public string IdentityDescription { get; set; }
    public Boolean? isCreateBack { get; set; }
    public string CreateBackUrl { get; set; }
    // DropDownList
    public List<Demanda> DemandaList { get; set; }
    public List<ObjetivosGrupales> ObjetivosGrupalesList { get; set; }
    public List<PoblacionObjetivo> PoblacionObjetivoList { get; set; }
    public List<Equipo> EquipoList { get; set; }
}

最后是执行方法的顺序:

 [AcceptVerbs(HttpVerbs.Post)]
    public override ActionResult AddTratamientoRealizadoPSIG(int masterId, CreateTratamientoRealizadoPSIGViewModel tratamientoRealizadoPSIGModel)
    {
        if (ModelState.IsValid)
        {
            TratamientoRealizadoPSIG tratamientoRealizadoPSIG = new TratamientoRealizadoPSIG();
            AutoMapper.Mapper.Map<CreateTratamientoRealizadoPSIGViewModel, TratamientoRealizadoPSIG>(tratamientoRealizadoPSIGModel, tratamientoRealizadoPSIG);

            Equipo equipo = _equipoService.Find(tratamientoRealizadoPSIG.Equipo.Id);
            **_equipoService.AddTratamientoRealizadoPSIG(equipo, tratamientoRealizadoPSIG, AuthenticatedUser);**

            return RedirectToAction("Edit", "Equipo", new { id = masterId });
        }
        else
        {
            List<Demanda> allDemanda = _demandaService.FindAll();
            List<ObjetivosGrupales> allObjetivosGrupales = _objetivosGrupalesService.FindAll();
            List<PoblacionObjetivo> allPoblacionObjetivo = _poblacionObjetivoService.FindAll();
            tratamientoRealizadoPSIGModel.DemandaList = allDemanda;
            tratamientoRealizadoPSIGModel.ObjetivosGrupalesList = allObjetivosGrupales;
            tratamientoRealizadoPSIGModel.PoblacionObjetivoList = allPoblacionObjetivo;
            ViewBag.MasterProperty = "Equipo";
            ViewBag.MasterId = masterId;
            ViewBag.Name = "AddTratamientoRealziadoPSIG";
            tratamientoRealizadoPSIGModel.isCreateBack = true;
            tratamientoRealizadoPSIGModel.CreateBackUrl = "~/Equipo/Edit/" + masterId;

            return View("~/Views/TratamientoRealizadoPSIG/Create.cshtml", tratamientoRealizadoPSIGModel);
        }
    }

public void AddTratamientoRealizadoPSIG(Equipo equipo, TratamientoRealizadoPSIG tratamiento, Usuario usuario)
    {
        tratamiento.UsuarioRegistro = usuario.IdentityDescription;
        **base.AddTratamientoRealizadoPSIG(equipo, tratamiento);**
    }

 public virtual void AddTratamientoRealizadoPSIG(Equipo equipo, TratamientoRealizadoPSIG tratamientoRealizadoPSIG)
    {
        equipo = EntitiesDB.EquipoSet.Find(equipo.Id);
        tratamientoRealizadoPSIG.Equipo = equipo;
        equipo.TratamientoRealizadoPSIG.Add(tratamientoRealizadoPSIG);
        tratamientoRealizadoPSIG.Demanda = EntitiesDB.EntryWithState(tratamientoRealizadoPSIG.Demanda, EntityState.Unchanged);
        tratamientoRealizadoPSIG.PoblacionObjetivo = EntitiesDB.EntryWithState(tratamientoRealizadoPSIG.PoblacionObjetivo, EntityState.Unchanged);
        tratamientoRealizadoPSIG.ObjetivosGrupales = EntitiesDB.EntryWithState(tratamientoRealizadoPSIG.ObjetivosGrupales, EntityState.Unchanged);
        **Change(equipo);**
    }

public virtual void Change(Equipo equipo)
    {
        EntitiesDB.Entry(equipo).State = EntityState.Modified;

        **EntitiesDB.SaveChanges();**
    }

SaveChanges() 抛出异常。正确创建数据库的关系。 有人可以帮我吗?

问候。

【问题讨论】:

  • InnerException 说:[SqlException(0x80131904):无法将值 NULL 插入到列 'Id',表 'BetislabDBv2_4.dbo.TratamientoRealizadoPSIG';列不允许空值。插入失败。声明已终止。]

标签: c# asp.net-mvc-3 entity-framework-4


【解决方案1】:

问题不在于源代码中实体之间的关系是自己定义的,而在于数据库本身。不表示要增加 Id,这是一个愚蠢的错误……但毕竟是错误。解决办法:

  • Design 中打开您的表格
  • 选择您的专栏并转到Column Properties
  • Indentity Specification 下,设置(Is Identity) = Yes 和
    Indentity Increment = 1

我在下面的帖子Why is SQL server throwing this error: Cannot insert the value NULL into column 'id'? 中看到了这个可能的解决方案,我尝试了所说的并解决了问题。

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多