using Microsoft.EntityFrameworkCore; namespace MyApplication.Models { public class MyContext : DbContext { public DbSetCustomers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("connection string"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity (entity => { entity.HasKey(e => e.Id); entity.Property(e => e.Name).IsRequired().HasMaxLength(50); }); } } public class Customer { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } } }
using Microsoft.EntityFrameworkCore; namespace MyApplication.Models { public class MyContext : DbContext { public DbSetIn this example, we are defining a more complex data model that includes multiple entities with various relationships. We have an Order entity that has a one-to-many relationship with OrderItem, a many-to-one relationship with Customer, and a many-to-many relationship with Product through the OrderItem entity. We also have a Product entity that has a many-to-one relationship with Category and a one-to-many relationship with OrderItem. Finally, we have a Category entity that has a one-to-many relationship with Product. Package library: Microsoft.EntityFrameworkCoreOrders { get; set; } public DbSet Products { get; set; } public DbSet Categories { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("connection string"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity (entity => { entity.HasKey(e => e.Id); entity.HasOne(e => e.Customer) .WithMany(c => c.Orders) .HasForeignKey(e => e.CustomerId) .OnDelete(DeleteBehavior.Restrict); }); modelBuilder.Entity (entity => { entity.HasKey(e => e.Id); entity.Property(e => e.Name).IsRequired().HasMaxLength(50); entity.HasOne(e => e.Category) .WithMany(c => c.Products) .HasForeignKey(e => e.CategoryId) .OnDelete(DeleteBehavior.SetNull); }); modelBuilder.Entity (entity => { entity.HasKey(e => e.Id); entity.Property(e => e.Name).IsRequired().HasMaxLength(50); }); } } public class Order { public int Id { get; set; } public int CustomerId { get; set; } public Customer Customer { get; set; } public ICollection OrderItems { get; set; } } public class OrderItem { public int Id { get; set; } public int OrderId { get; set; } public Order Order { get; set; } public int ProductId { get; set; } public Product Product { get; set; } public int Quantity { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int? CategoryId { get; set; } public Category Category { get; set; } } public class Category { public int Id { get; set; } public string Name { get; set; } public ICollection Products { get; set; } } public class Customer { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public ICollection Orders { get; set; } } }