many to many relationship ef core code example
Example 1: ef core many-to-many
class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasMany(p => p.Tags)
.WithMany(p => p.Posts)
.UsingEntity<PostTag>(
j => j
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId),
j => j
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId),
j =>
{
j.Property(pt => pt.PublicationDate).HasDefaultValueSql("CURRENT_TIMESTAMP");
j.HasKey(t => new { t.PostId, t.TagId });
});
}
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public ICollection<Tag> Tags { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class Tag
{
public string TagId { get; set; }
public ICollection<Post> Posts { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class PostTag
{
public DateTime PublicationDate { get; set; }
public int PostId { get; set; }
public Post Post { get; set; }
public string TagId { get; set; }
public Tag Tag { get; set; }
}
Example 2: many to many ef core
public class Book{ public int BookId { get; set; } public string Title { get; set; } public Author Author { get; set; } public ICollection<BookCategory> BookCategories { get; set; }} public class Category{ public int CategoryId { get; set; } public string CategoryName { get; set; } public ICollection<BookCategory> BookCategories { get; set; }} public class BookCategory{ public int BookId { get; set; } public Book Book { get; set; } public int CategoryId { get; set; } public Category Category { get; set; }}
Example 3: many to many ef core
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace EFGetStarted
{
public class Context : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite(@"Data Source=blogging.db");
protected override void OnModelCreating(ModelBuilder modelBuilder) { }
public DbSet<GarageDoorModel> GarageDoorModels { get; set; }
public DbSet<Section> Sections { get; set; }
}
public class GarageDoorModel
{
public int GarageDoorModelId { get; set; }
public string ModelName { get; set; }
public ICollection<Section> Sections { get; set; }
}
public class Section
{
public int SectionId { get; set; }
public string SectionName { get; set; }
public string Key { get; set; }
public ICollection<GarageDoorModel> GarageDoorModels { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using EFGetStarted;
namespace SchoolCourse
{
internal class Program
{
private static void Main()
{
using var db = new Context();
Console.WriteLine("Inserting new records");
var modelName = "X";
var sections = new List<Section> {
new Section
{
SectionName = "Engine",
Key = "front_engine"
},
};
db.Add(new GarageDoorModel {ModelName = modelName});
db.SaveChanges();
var gdm = db.GarageDoorModels.First(g => g.ModelName == modelName);
gdm.Sections = sections;
db.SaveChanges();
var new_gdm = db.GarageDoorModels.First(g => g.ModelName == modelName);
Console.WriteLine(new_gdm.Sections);
foreach (var VARIABLE in new_gdm.Sections)
{
Console.WriteLine(VARIABLE.Key);
}
}
}
}
Project 'SchoolCourse' has the following package references
[net5.0]:
Top-level Package Requested Resolved
> Microsoft.EntityFrameworkCore.Design 5.0.5 5.0.5
> Microsoft.EntityFrameworkCore.Sqlite 5.0.5 5.0.5
> Microsoft.EntityFrameworkCore.SqlServer 5.0.5 5.0.5
Example 4: many to many ef core
protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity<BookCategory>() .HasKey(bc => new { bc.BookId, bc.CategoryId }); modelBuilder.Entity<BookCategory>() .HasOne(bc => bc.Book) .WithMany(b => b.BookCategories) .HasForeignKey(bc => bc.BookId); modelBuilder.Entity<BookCategory>() .HasOne(bc => bc.Category) .WithMany(c => c.BookCategories) .HasForeignKey(bc => bc.CategoryId);}
Example 5: one to many relationship ef core
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public Company Company { get; set; }
}
Example 6: many to many ef core
public class Book{ public int BookId { get; set; } public string Title { get; set; } public Author Author { get; set; } public ICollection<Category> Categories { get; set; }} public class Category{ public int CategoryId { get; set; } public string CategoryName { get; set; } public ICollection<Book> Books { get; set; }}