Entity Framework SaveChanges error details

后端 未结 5 1293
攒了一身酷
攒了一身酷 2021-02-18 14:35

When saving changes with SaveChanges on a data context is there a way to determine which Entity causes an error? For example, sometimes I\'ll forget to assign a da

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-18 14:55

    I think I might make separate calls to SaveChanges(). That is usually what I do for exactly this reason. Can I ask why you are saving multiple entities at a time? If you have to, I would follow the other guy's advice and validate the entities beforehand.

    Or maybe there is a better way to structure your code so that in valid entiies won't even be attempted to be saved. Maybe detach your entities, and then run them through a validation method before attaching them to the new context. Hope that helps!

提交回复
热议问题