Some keypoints are covered here.
It provides the interaction between the data model and the database schema. It stores the necessary information to handle the tasks on runtime such as building queries, commands to push the data to the database, transforming database results into your objects.
Also, when a change is made to the data model and/or the db context, EF Core has to know about that diff to handle the database operations appropriately so migration must be run after each change before proceeding.
- CLI: There are two ways to achieve:
- To consume through Visual Studio Package Manager Console: Add reference to
Microsoft.EntityFrameworkCore.Tools dotnetcli:
# Install dotnet-ef dotnet tool install --global dotnet-ef # Use dotnet-ef commands
- To consume through Visual Studio Package Manager Console: Add reference to
- API: Add reference to
Microsoft.EntityFrameworkCore.Design.
Microsoft.EntityFrameworkCore.Toolsalready has a dependency to theMicsrosoft.EntityFrameworkCore.Designpackage.
It can be considered as in-memory representation of the of a database record. The type of a DbSet is referred as entity and it is included EF Core model by default. It stores data and usually does not include bussiness logic in method form.
If one wants to exclude your
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore< <entity_type_to_ignore> >();
}Please check Relationships section for details.
Key is the unique identifier an EF Core entity. Any property in an entity can be defined as key.
Primary Key is the unique identifier for a record in a relational database table. As Microsoft documentation states:
By convention, a property named
Idor<type name>Idwill be configured as the primary key of an entity.
It can also be defined explicitly through Data Annotation or Fluent API.
// Fluent API
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasKey(u => u.Id);
}
Multiple properties can be configured as primary key which is called composite key. This can be done through Fluent API.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasKey(s=> new {s.Id, s.Name});
}The type of the Primary Key is usually selected as int, System.Guid or byte[].
Foreign Key is an identifier that provides the link between two tables. It is usually the Primary Key of the other end of the relationship. It can be defined in several ways and that definition depends on the relationship. It will be explained in Relationships section.
- Better for faster queries but when it is optimized well. Not everytime.
- Requires additional storage to hold the pointer information for the table it points to
- Efficient when having an index on Foreign KEy for join situations
UPDATE (EF Core 3.0 and after): The default property access mode changed from
PreferFieldDuringConstructiontoPreferField.
A property can be stored in the change tracker, rather than in the entity's CLR type. This type of property is called Shadow Property.
You can also create a conceptual property in your model that does not have a corresponding CLR property in the entity class, but instead uses a field to store the data in the entity. Field-only properties are commonly used when the entity class uses methods instead of properties to get/set values, or in cases where fields shouldn't be exposed at all in the domain model (e.g. primary keys).
It is recommended that you use immutable types (classes or structs) with value converters when possible. This is usually more efficient and has cleaner semantics than using a mutable type.
n order to understand how EF Core tracks these objects, it is useful to know that a primary key is created as a shadow property for the owned type. The value of the key of an instance of the owned type will be the same as the value of the key of the owner instance
Owned types need a primary key. If there are no good candidates properties on the .NET type, EF Core can try to create one. However, when owned types are defined through a collection, it isn't enough to just create a shadow property to act as both the foreign key into the owner and the primary key of the owned instance, as we do for OwnsOne: there can be multiple owned type instances for each owner, and hence the key of the owner isn't enough to provide a unique identity for each owned instance.
Use no ctor defined or public or protected ctors with arguments to initialize non navigation properties with private set.
- Injecting services: Only EFCore supported services can be injected
Jing Wang - Database Index
StackOverflow question
Jimmy Farillo - The Basics of Database Indexes For Relational Databases