简体   繁体   English

C#EntityFramework模型更新错误

[英]C# EntityFramework model update error

My project is web apllication on ASP.NET MVC 6 and basicaly I have a realy weird problem. 我的项目是ASP.NET MVC 6上的Web应用程序,基本上我有一个很奇怪的问题。

This is the code: 这是代码:

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

methods don't do anything specific but save modified states to database. 方法不执行任何特定操作,而是将修改后的状态保存到数据库中。

Here is what the problem is. 这是问题所在。 When I start application normally and run through this part of the code it throws error: 当我正常启动应用程序并运行代码的这一部分时,它将引发错误:

Validation failed for one or more entities. 一个或多个实体的验证失败。 See 'EntityValidationErrors' property for more details. 有关更多详细信息,请参见'EntityValidationErrors'属性。

But here is the tricky part when I debug it and go step by step it works just fine without any error. 但是,当我调试它并逐步进行时,这是棘手的部分,它工作正常,没有任何错误。

You may want to take a look at this to find more information about your exception: 您可能需要看一下它,以查找有关您的异常的更多信息:

Validation failed for one or more entities. 一个或多个实体的验证失败。 See 'EntityValidationErrors' property for more details 有关更多详细信息,请参见“ EntityValidationErrors”属性

Thank you for your help. 谢谢您的帮助。 It would seem that the problem was in dashboard model. 看来问题出在仪表板模型中。 Lazzy loading didnt load my User property and since it is foreign key it can not be null value. 大量加载未加载我的User属性,并且由于它是外键,因此不能为空值。

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

Any way thank you for your help. 无论如何,谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM