C#EntityFramework模型更新错误

时间:2015-08-24 18:40:14

标签: c# entity-framework

我的项目是ASP.NET MVC 6上的Web应用程序 基本上,我有一个非常奇怪的问题。

这是代码:

await Dashboards.UpdateReward(dashboard);
await Lessons.Update(lesson);

方法不会执行任何特定操作,只会将修改后的状态保存到数据库中。

这就是问题所在。当我正常启动应用程序并运行这部分代码时,它会抛出错误:

  

一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

但是当我调试它时,这是一个棘手的部分,并且一步一步地运行它没有任何错误。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助。似乎问题出在仪表板模型中。 Lazzy加载没有加载我的User属性,因为它是外键,它不能是null值。

    [Key, ForeignKey("User")]
    public string UserId { get; set; }

    //Gemification
    public int Level { get; set; }
    public int Experience { get; set; }
    public int Yens { get; set; }

    //Application
    [Column(TypeName = "datetime2")]
    public DateTime Created { get; set; }
    [Column(TypeName = "datetime2")]
    public DateTime Updated { get; set; }
    public string CreatedBy { get; set; }
    public string UpdatedBy { get; set; }

    public virtual ICollection<Lesson> Lessons { get; set; }
    public virtual ICollection<OwnedGroup> OwnedGroups { get; set; }
    [Required]
    public virtual ApplicationUser User { get; set; }

谢谢你的帮助。

相关问题