Junior — Middle
Can you explain what Entity Framework Core is and how it is used for working with databases in .NET applications?
sobes.tech AI
Answer from AI
Entity Framework Core (EF Core) is an Object-Relational Mapper (ORM) from Microsoft for .NET applications. It allows working with a database through objects and classes without directly using SQL queries.
Main features of EF Core:
- Mapping C# classes to database tables.
- Performing CRUD operations (create, read, update, delete) via LINQ queries.
- Support for migrations to manage database schema changes.
Example of using EF Core:
public class ApplicationContext : DbContext
{
public DbSet<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
// Adding a new user
using (var context = new ApplicationContext())
{
var user = new User { Name = "Ivan" };
context.Users.Add(user);
context.SaveChanges();
}
Thus, EF Core simplifies working with databases, allowing developers to work with data as objects rather than tables and SQL.