gpt4 book ai didi

c# - 如何忽略服务器端的 EntityValidationErrors

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:48 25 4
gpt4 key购买 nike

模型代码:

public partial class Content
{
public int ID { get; set; }

[Required(ErrorMessage = "Required.")]
public string ContentText1 { get; set; }
}

该 View 不包含任何与内容相关的字段。

保存时的服务器端代码( Controller 代码):

Content c = new Model.Content();
db.Contents.Add(c);
db.Entry(pc).State = System.Data.Entity.EntityState.Added;
db.SaveChanges();

这会导致错误:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

是否可以在不更改模型的情况下避免此错误?

最佳答案

将此语句放在数据库的构造函数中,该类继承自 DbContext

base.Configuration.ValidateOnSaveEnabled = false;

关于c# - 如何忽略服务器端的 EntityValidationErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41311674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com