Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core doesn't validate data when saving?

I have the following remote validation rule:

[AcceptVerbs("Get", "Post")]
public IActionResult ValidateWindowEndDate(DateTime? endDate, DateTime? startDate)
{
    int minWeeks = 8;

    if (startDate.HasValue && endDate.HasValue
            && (endDate < startDate.Value.AddDays(minWeeks * 7)))
    {
        return Json(data: $"Inspection window end date must be at least {minWeeks} weeks after start date.");
    }

    return Json(data: true);
}

That's linked to a property in my class as follows:

[Required]
[Remote("ValidateWindowEndDate", "InspectionWindow", AdditionalFields = "StartDate")]
public DateTime EndDate { get; set; }

When I enter an invalid data in my view, the validation occurs as expected. But, if I get an instance of the object in code, then change the date to an invalid one and save it back to the database, e.g.

luInspectionWindow.EndDate = luInspectionWindow.StartDate.AddDays(1);
_context.Update(luInspectionWindow);
await _context.SaveChangesAsync();

then the save takes place without an exception and the validation method is never called, which wasn't the behaviour I was expecting. I thought EF would reject the record as invalid? Is this the expected behaviour?

like image 511
tomRedox Avatar asked Apr 15 '17 12:04

tomRedox


People also ask

How does Entity Framework save data?

Insert Data Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context. Students. Add(std) adds a newly created instance of the Student entity to a context with Added EntityState.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

How do I validate a model in Entity Framework?

Entity Framework provides a great variety of validation features that can feed through to a user interface for client-side validation or be used for server-side validation. When using code first, you can specify validations using annotation or fluent API configurations.

Does Entity Framework core cache?

Entity Framework Core does not come with its caching framework (like NHibernate does). However, NCache has developed a very flexible, powerful, and yet very simple caching framework for you.


1 Answers

So, this turns out to be a change in the way EF works in Core. It now no longer does validation at all, that is left up to the Client and server (via Model.State). Rowan Miller explains that decision in the EF repo as follows:

In EF Core we dropped doing validation since it is usually already done client-side, server-side, and then in the database too.

I can't use the model state in my particular scenario, so instead I've tried to get Remote validation working manually via the Validator object, but I'm not having any luck with that either. More on that in this question: Validator.TryValidateObject does not call Remote validation.

like image 78
tomRedox Avatar answered Sep 28 '22 02:09

tomRedox