ASP — ASP.NET Entity Framework
DbContext
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
}
Model
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime CreatedAt { get; set; }
}
Register Context
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
CRUD Operations
// Create
var user = new User { Name = "John", Email = "john@example.com" };
context.Users.Add(user);
await context.SaveChangesAsync();
// Read
var users = await context.Users.ToListAsync();
var user = await context.Users.FindAsync(1);
// Update
user.Name = "Jane";
await context.SaveChangesAsync();
// Delete
context.Users.Remove(user);
await context.SaveChangesAsync();
Mini Practice
- Create DbContext
- Define models
- Perform CRUD operations
- Use async methods
Up Next
Continue with ASP.NET Dependency Injection — DI.
Related Topics
Frequently Asked Questions about ASP.NET Entity Framework
What is ASP.NET Entity Framework in ASP?
ASP.NET Entity Framework is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn ASP.NET Entity Framework?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn ASP.NET Entity Framework.
Why is ASP.NET Entity Framework important in ASP?
ASP.NET Entity Framework is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.