1
votes

Model Code:

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

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

The view doesn't contain any Field related to Content.

Server side code on save (Controller Code):

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

This results in an error:

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.

Is it possible to prevent this error without changing the model?

1
Why do you want to do this? Why not just remove the required attribute if you don't want to validate that? - nemesv
Its already used in multiple views. If I remove validation then I need to add validations manually in that views. - Dev Kumar
If you add a new Content object and ContentText1 is required then it must be validated. It's a more common scenario to want to skip validation on some properties when updating. - Gert Arnold

1 Answers

6
votes

Put this statement in the constructor of the DB, the class which inherit from DbContext

base.Configuration.ValidateOnSaveEnabled = false;