How to clear scan results in a Framework object

Can I delete validation results in an ObjectContext related to the Entity Framework? I wanted something like this code:

public void ClearValidationResults (ObjectContext db) { var dbContext = new DbContext(db, true); var validationResults = dbContext.GetValidationErrors(); validationResults.Clear(); } 

I want to implement this functionality for use in unit tests to validate validation rules without requiring changes to be saved to the database. Thanks.

+4
source share
3 answers

I solve the problem by creating a new ObjectContext every time I need to clear validation errors. This is not the most elegant solution, but it works. Since this process is in the context of unit tests, and unit tests are fast enough, I will maintain this code until a better solution appears.

 public void ClearValidationResults (ObjectContext db) { db = new MyObjectContext(); } 
-2
source

DbContext does not store validation errors; it validates entities every time you call DbContext.GetValidationErrors() or DbContext.SaveChanges() . So, if you have an invalid object that is being monitored by your context, DbContext.GetValidationErrors() will always return errors. You need to separate or correct an invalid object / entities, and the error will disappear, since invalid objects tracked by your context will not be found.

+5
source

Clear local Entity storage.

When we add Entity to Collection, we add it to the local storage, and we continue to receive errors, since the local storage of this object is fuzzy and still has old records. This way you clear local Entity lines.

 dbContext.EntityName.Local.Clear(); 

+2
source

Source: https://habr.com/ru/post/1502027/


All Articles