简体   繁体   中英

C# EntityFramework model update error

My project is web apllication on ASP.NET MVC 6 and basicaly I have a realy weird problem.

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.

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

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.

    [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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