Junior — Middle
Explain the principles of operation and main features of Entity Framework Core as an ORM tool for .NET applications.
sobes.tech AI
Answer from AI
Entity Framework Core (EF Core) is an ORM (Object-Relational Mapper) for .NET that allows working with a database through objects and classes without directly using SQL queries.
Main features of EF Core:
- Support for LINQ to write database queries in C#.
- Change tracking of objects and automatic generation of SQL commands for database updates.
- Migrations — a mechanism for managing database schema changes.
- Cross-platform support and compatibility with various DBMS (SQL Server, SQLite, PostgreSQL, etc.).
Working principle:
- Define model classes that correspond to tables.
- Create a data context (DbContext) that manages the connection and objects.
- Use LINQ queries for selecting, adding, updating, and deleting data.
Example:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
using(var context = new AppDbContext())
{
var products = context.Products.Where(p => p.Name.Contains("book")).ToList();
}