Get ready for your next interview with our comprehensive question library
EF Core supports three main approaches:
Scaffold-DbContext
commandAn Entity is a class that represents a table in the database. Each instance of an entity corresponds to a row in the table.
public class Product
{
public int Id { get; set; } // Primary key
public string Name { get; set; }
public decimal Price { get; set; }
public int CategoryId { get; set; }
// Navigation property
public Category Category { get; set; }
}
Requirements for an entity:
DbContext
is the primary class responsible for interacting with the database. It represents a session with the database and acts as a Unit of Work pattern implementation.
Key responsibilities:
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection-string");
}
}
DbSet<T>
represents a collection of entities of a specific type that can be queried from the database. It provides an abstraction over database tables.
Key features:
// Querying
var products = context.Products.Where(p => p.Price > 100).ToList();
// Adding
context.Products.Add(new Product { Name = "New Product" });
// Removing
context.Products.Remove(product);
// Local entities
var localProducts = context.Products.Local.ToList();
Task<int>
// Synchronous
int changesCount = context.SaveChanges();
// Asynchronous
int changesCount = await context.SaveChangesAsync();
Benefits of async:
Data Annotations are attributes applied to entity classes and properties to configure the model and validation rules.
Common annotations:
public class Product
{
[Key]
public int ProductId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }
[NotMapped]
public string DisplayName { get; set; }
}
Categories:
[Key]
, [DatabaseGenerated]
[Required]
, [MaxLength]
, [Range]
[Table]
, [Column]
, [NotMapped]
[ForeignKey]
, [InverseProperty]
Migrations are a way to incrementally update the database schema to keep it in sync with the entity model while preserving existing data.
Benefits:
# Add migration
dotnet ef migrations add InitialCreate
# Update database
dotnet ef database update
# Remove last migration
dotnet ef migrations remove
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime