locked
Validation failed for one or more entities. RRS feed

  • Question

  • User-211498538 posted

    Hi, 

    The problem I have is that EF tries to apply the validation in the controller. Sortcode1, 2, 3 and AccountNo are converted into encrypted values, which conflicts with the RegularExpression and StringLength validation defined in the model. 

    Does anyone know how to solve my programming knightmare?

    FYI - My error suggests; Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.


    This is my controller action.

      [HttpPost]
            public ActionResult Create(Section2 section2, string BankName, string SortCode1, string SortCode2, string SortCode3, string AccountNo)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        var s = new SupplierContext();
                        string strGuid = Convert.ToString((Session["guid"]));
                        var md = s.sMasterDatas.FirstOrDefault(m => m.GUID == strGuid);
       
                        // Standard section inserts
                        Crypto crypto = new Crypto();
    
                        section2.BankName = crypto.Encrypt(BankName);
                        section2.SortCode1 = crypto.Encrypt(SortCode1);
                        section2.SortCode2 = crypto.Encrypt(SortCode2);
                        section2.SortCode3 = crypto.Encrypt(SortCode3);
                        section2.AccountNo = crypto.Encrypt(AccountNo);
                        
                        section2.ModifiedDate = DateTime.Now;
                        section2.RecStatus = 1;
                        section2.MasterDataID = md.MasterDataID;
                        section2.IsCompleted = 1;
                        section2.SubmittedBy = User.Identity.Name;
    
                        db.Section2s.Add(section2);
                        db.SaveChanges();
                    }
    
                    catch (DbEntityValidationException dbEx)
                    {
                    }
    
                    return RedirectToAction("Index", "Home");
                }
    
                ViewBag.TradeID = new SelectList(db.Trades, "TradeID", "Actual", section2.TradeID);
                ViewBag.MasterDataID = new SelectList(db.sMasterDatas, "MasterDataID", "GUID", section2.MasterDataID);
    
                return View(section2);
            }

    This is the model that is relevant.

            [Required(ErrorMessage = "Required")]
            [RegularExpression(@"^\d+$", ErrorMessage = "Numbers only")]
            [StringLength(2, MinimumLength = 2, ErrorMessage = "Must be two numbers")]
            [Display(Name = "Sort Code 1")]
            public string SortCode1 { get; set; }
    
            [Required(ErrorMessage = "Required")]
            [RegularExpression(@"^\d+$", ErrorMessage = "Numbers only")]
            [StringLength(2, MinimumLength = 2, ErrorMessage = "Must be two numbers")]
            [Display(Name = "Sort Code 2")]
            public string SortCode2 { get; set; }
    
            [Required(ErrorMessage = "Required")]
            [RegularExpression(@"^\d+$", ErrorMessage = "Numbers only")]
            [StringLength(2, MinimumLength = 2, ErrorMessage = "Must be two numbers")]
            [Display(Name = "Sort Code 3")]
            public string SortCode3 { get; set; }
    
            [Required(ErrorMessage = "Required")]
            [RegularExpression(@"^\d+$", ErrorMessage = "Numbers only")]
            [StringLength(8, MinimumLength = 8, ErrorMessage = "Must be 8 characters")]
            [Display(Name = "Account Number")]
            public string AccountNo { get; set; }




    Thursday, July 26, 2012 9:05 AM

Answers

  • User-1652363262 posted

    that's no model validation error that's a database error check if you got fields in the table that are not nullable but which aren't filled in your form or model check for null values with a breakpoint on your action and hover over the model in the parameters

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 27, 2012 4:02 PM
  • User-211498538 posted

    You were the closest to solving the problem. 

    However, I worked it out. It's a known fault with EF 4.1.

    http://blogs.msdn.com/b/adonet/archive/2011/03/29/ef-4-1-rtw-change-to-default-maxlength-in-code-first.aspx?

    I set the MaxLength to 50 (leaving the StringLength alone) and set dbContext.Configuration.ValidateOnSaveEnabled = false;

    Thanks for everyones support. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 30, 2012 5:19 AM

All replies

  • User197322208 posted

    The problem I have is that EF tries to apply the validation in the controller.

    Not EF applies, buyt ModelState.ISvalid.

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    Which line?

    ANd why you have this

    catch (DbEntityValidationException dbEx)                  {                  } 
    ?

    ( Advice:remove!)

    Thursday, July 26, 2012 9:45 AM
  • User-211498538 posted

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    This error appears in the exception. When debugged it dies at db.SaveChanges();


    catch (DbEntityValidationException dbEx)     

    The exception returns...

    + [0] {System.Data.Entity.Validation.DbValidationError} System.Data.Entity.Validation.DbValidationError

    This array insists there are 8 Validation errors. Obviously 2 for each field.

    Thursday, July 26, 2012 9:56 AM
  • User197322208 posted

    See 'EntityValidationErrors' property for more details

    Tell one of 'EntityValidationErrors' 

    Thursday, July 26, 2012 10:41 AM
  • User-211498538 posted

    Below is [0] error of 7.

    ErrorMessage = "Must be two numbers"

    PropertyName = "SortCode1"


    Do I need something like this?

    context.Configuration.ValidateOnSaveEnabled = false;



    Thursday, July 26, 2012 10:48 AM
  • User197322208 posted

    Below is [0] error of 7.

    ErrorMessage = "Must be two numbers"

    PropertyName = "SortCode1"

    That is yours specific code. Try to find in the source. 

    Thursday, July 26, 2012 12:39 PM
  • User-211498538 posted

    The source error is Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    This is why I am stuck.

    Friday, July 27, 2012 3:56 AM
  • User1034446946 posted

    Have you used the debugger to find what sort code info is being send to be saved? I suggest either your nor putting in 2 digits or maybe your regex is changing the 2 digts, or at some point between you is model valid and save changes the sort code information is being change to not match the rules.

    Friday, July 27, 2012 9:42 AM
  • User-1652363262 posted

    not to be idiotic but isn't the encryption there a bit late? you already made an unencrypted post to your controller...

    for the validation you should use clientvalidation jquery unobstrusive with a viewmodel 

    but encryption should be done before the post action what makes server validation hardly possible but that doesn't really matter if your client validation is working well 

    use some javascript/ajax mix to encrypt the data and post encrypted data to your action

    anyway my point is just somebody on the same network who's sniffing the network would pick up the unencrypted value's of the post before you even encrypt it, now you might say the data hasn't come from my application or db... 

    oh in your view put this 

    <script type="text/javascript">
        $.validator.unobtrusive.parse("#formid");
    </script>

    and delete the if(model.isvalid) from your controller now you got nice clientvalidation instead of server validation

    Friday, July 27, 2012 9:47 AM
  • User-211498538 posted

    Hey thanks for your advice.

    The application will be on a SSL, which should keep it safe?

    I will have another attempt at resolving this, then perhaps if I fail, I will go with your solution.

    David

    Friday, July 27, 2012 10:17 AM
  • User-211498538 posted

    Exeption Snapshot:

    Source = EntityFramework.

    StackTrace = 

    at System.Data.Entity.Internal.InternalContext.SaveChanges()
    at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
    at System.Data.Entity.DbContext.SaveChanges()
    at KeySupplierSystem.Controllers.Section2Controller.Create(Section2 section2, String BankName, String SortCode1, String SortCode2, String SortCode3, String AccountNo) in C:\Grontmij VSS\Shared Services\Health and Safety\KeySupplierSystem\KeySupplierSystem\Controllers\Section2Controller.cs:line 82

    InnerException = null

    Message = Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

    TargetSite = {Int32 SaveChanges()}

    Friday, July 27, 2012 10:45 AM
  • User-1652363262 posted

    that's no model validation error that's a database error check if you got fields in the table that are not nullable but which aren't filled in your form or model check for null values with a breakpoint on your action and hover over the model in the parameters

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 27, 2012 4:02 PM
  • User-211498538 posted

    You were the closest to solving the problem. 

    However, I worked it out. It's a known fault with EF 4.1.

    http://blogs.msdn.com/b/adonet/archive/2011/03/29/ef-4-1-rtw-change-to-default-maxlength-in-code-first.aspx?

    I set the MaxLength to 50 (leaving the StringLength alone) and set dbContext.Configuration.ValidateOnSaveEnabled = false;

    Thanks for everyones support. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 30, 2012 5:19 AM