Last active
June 13, 2026 12:23
-
-
Save sunmeat/7c880ba7407b5218ecf337a895f3421b to your computer and use it in GitHub Desktop.
автоматично створена проміжкова таблиця
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AppDbContext.cs: | |
| using EFCoreCodeFirst.Models; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.Extensions.Configuration; | |
| namespace EFCoreCodeFirst | |
| { | |
| public class AppDbContext : DbContext | |
| { | |
| public DbSet<User>? Users => Set<User>(); | |
| public DbSet<Order>? Orders => Set<Order>(); | |
| public DbSet<Product>? Products => Set<Product>(); | |
| public DbSet<Category>? Categories => Set<Category>(); | |
| public DbSet<Person>? People { get; set; } | |
| public DbSet<Hobby>? Hobbies { get; set; } | |
| // !!! проміжковий датасет видалено! | |
| public AppDbContext() | |
| { | |
| } | |
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
| { | |
| if (!optionsBuilder.IsConfigured) | |
| { | |
| var configuration = new ConfigurationBuilder() | |
| .SetBasePath(Directory.GetCurrentDirectory()) | |
| .AddJsonFile("appsettings.json") | |
| .Build(); | |
| optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection")); | |
| } | |
| } | |
| protected override void OnModelCreating(ModelBuilder modelBuilder) | |
| { | |
| base.OnModelCreating(modelBuilder); | |
| // автоматичне створення проміжкової таблиці для Many-to-Many !!! | |
| modelBuilder.Entity<Person>() | |
| .HasMany(p => p.Hobbies) | |
| .WithMany(h => h.People) | |
| .UsingEntity(j => j.ToTable("PersonHobbies")); | |
| } | |
| } | |
| } | |
| ====================================================================================================================== | |
| Person.cs: | |
| namespace EFCoreCodeFirst.Models | |
| { | |
| public class Person | |
| { | |
| public int PersonId { get; set; } // PK | |
| public string? Name { get; set; } | |
| public ICollection<Hobby> Hobbies { get; set; } = new List<Hobby>(); // Many-to-Many !!! | |
| } | |
| } | |
| ====================================================================================================================== | |
| Hobby.cs: | |
| namespace EFCoreCodeFirst.Models | |
| { | |
| public class Hobby | |
| { | |
| public int HobbyId { get; set; } // PK | |
| public string? Name { get; set; } | |
| public ICollection<Person> People { get; set; } = new List<Person>(); // Many-to-Many !!! | |
| } | |
| } | |
| ====================================================================================================================== | |
| Program.cs: !!! | |
| using EFCoreCodeFirst.Models; | |
| using Microsoft.EntityFrameworkCore; | |
| namespace EFCoreCodeFirst | |
| { | |
| class Program | |
| { | |
| static async Task Main() | |
| { | |
| Console.Title = "Міграції"; | |
| Console.OutputEncoding = System.Text.Encoding.UTF8; | |
| await using var db = new AppDbContext(); | |
| Console.WriteLine("Перевірка та застосування міграцій..."); | |
| await db.Database.MigrateAsync(); | |
| Console.WriteLine("Міграції застосовані (або вже були актуальні).\n"); | |
| var hobby1 = new Hobby { Name = "Футбол" }; | |
| var hobby2 = new Hobby { Name = "Програмування" }; | |
| var hobby3 = new Hobby { Name = "Читання" }; | |
| var hobby4 = new Hobby { Name = "Шахи" }; | |
| var hobby5 = new Hobby { Name = "Спорт" }; | |
| var person1 = new Person { Name = "Олександр", Hobbies = new List<Hobby> { hobby1, hobby3 } }; | |
| var person2 = new Person { Name = "Руслан", Hobbies = new List<Hobby> { hobby2, hobby4 } }; | |
| var person3 = new Person { Name = "Максим", Hobbies = new List<Hobby> { hobby5, hobby1 } }; | |
| var person4 = new Person { Name = "Карина", Hobbies = new List<Hobby> { hobby2, hobby5 } }; | |
| var person5 = new Person { Name = "Аліса", Hobbies = new List<Hobby> { hobby3, hobby4 } }; | |
| db.AddRange(person1, person2, person3, person4, person5); | |
| db.SaveChanges(); | |
| using (var context = new AppDbContext()) | |
| { | |
| var peopleWithHobbies = context.People?.Include(p => p.Hobbies).ToList(); | |
| foreach (var person in peopleWithHobbies!) | |
| { | |
| Console.WriteLine($"{person.Name}"); | |
| foreach (var hobby in person.Hobbies) | |
| { | |
| Console.WriteLine($" - Захоплення: {hobby.Name}"); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment