The “DbContext.GetValidationErrors()” method gives you the explicit control of when validating entities. But EF performs the validations autoutomatically when the “SaveChanges()” method is called.
Validating a single entity object is is quite useful in a web applications. In most web applications, the entity object is processed as soon as it is added or modified. But in some applications, the batch processing can be a better option. In a batch mode, you need to deal with multiple entity objects.
When you configure entity properties with data annotations or Fluent API, DbContext performs the validation of them. EF provides necessary types for validation and they are called Validation API.
When you work with data, the repository pattern is very useful. Rather than directly accessing data sources, you can separate business logic from data access logic. Business classes does not even know where the data come from.
When you use EF, what you are doing is just manipulating a collection with objects. When “SaveChanges()” is called, all the changes are converted into corresponding SQL statements, which were executed in the database.
Retrieving data from database is one of the most important tasks in database programming. DbSet is designed to work with LINQ to Entities, therefore if you are familiar to LINQ you do not have any problems.
When you are working with DbContext, you are actually working with entity sets. DbSet represents a typed entity set that is used to perform create, read, update, and delete operations. You are not creating DbSet objects and using them indepedently. DbSet can be only used with DbContext.
The main controller of EF(Entity Framework) is “System.Data.Objects.ObjectContext” class. When Code First was introduced, “DbContext” was also introduced.
“System.Data.Entity.DbContext” is a stripped-down version of ObjectContext. DbContext makes developers to use more frequently used features more easily. When you use Code First, you will use DbContext too.