Up()
and Down()
. This allows you to move up and down the chain of migrations, thereby controlling the version of the database. The Seed(DbContext)
method allows you to add the necessary data to initialize the database. This is a great way to quickly make changes to the database, respond to new requirements or solve problems.AlterStoredProcedure
method. Many C # developers would gladly refuse to keep them, because maintaining them is a little more difficult. But you cannot get away from them, because if all operations are implemented in code, after what time the application will start to work very slowly, and requests to the database will be performed slowly.Lazy loading
automatically loads automatic entities or collections that are associated directly with the entity for which a query is being made to the database. This approach has drawbacks related to the fact that we literally pull out everything related to the records we need, while EF will put them in context and track changes to these records. To put it mildly, this can cause a number of problems and inconveniences. public class Category { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Product> Products { get; set; } }
Eager loading
- allows you to load the necessary entities as part of the request using the Include
method. var categories = context.Categories .Include(p => p.Products).ToList();
Explicit Loading
is a way to explicitly pull up the necessary entities if Lazy Loading has been disabled. var category = context.categories.Find(1); context.Entry(category).Collection(p => p.Products).Load();
System.ComponentModel.DataAnnotations (DataAnnotation)
- a way to customize the model so that both EF and ASP.NET MVC understand the configuration rules. DataAnnotation
allows you to specify validation rules or simply describe primary and foreign keys using attributes ( [Key], [RoreignKey], [Index], [StringLength], [Required], [NotMapped]
and others).System.Data.Entity.ModelConfiguration.Conventions (Code firs conventions)
- a series of rules that apply to the model and describe it using the C # classes. You can specify navigation properties, primary and foreign keys, complex types.OptimisticConcurrencyException
thrown by the Entity Framework, but first you need to teach EF to understand when a conflict has occurred. To do this, you need to add a property with the help of which you could monitor the versions (usually rowversion
), and then indicate that this is a ConcurencyToken
. This can be done through the IsConcurrencyToken
method, indicating which property should be monitored.DbContext.Database.Log
, which accepts a string as a parameter. It will process this line, for example, it will simply save logs to a file or output it to the console. In essence, logging is just interception of commands, and if you need to implement your own logic, then you need an IDbCommandInterceptor
implementation with which you can subscribe to events and do the necessary operations with the database context.IDbConnectionStrategy
.Source: https://habr.com/ru/post/273755/
All Articles