-
-
Save JansthcirlU/fe95b15e7cd200e707853d0b6c4528ff to your computer and use it in GitHub Desktop.
Link table with the names of the linked entities
This file contains 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
public class ABDbContext : DbContext | |
{ | |
public DbSet<ClassA> ClassAs { get; set; } | |
public DbSet<ClassB> ClassBs { get; set; } | |
public DbSet<ABLink> ABLinks { get; set; } | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
// Custom many-to-many, see: https://dev.to/shibayan/tried-entity-framework-core-5-0-s-many-to-many-support-4och | |
modelBuilder | |
.Entity<ClassA>() | |
.HasMany(a => a.ClassBs) | |
.WithMany(b => b.ClassAs) | |
.UsingEntity<ABLink> | |
( | |
link => link.HasOne(l => l.ClassA).WithMany(), | |
link => link.HasOne(l => l.ClassB).WithMany() | |
) | |
.HasKey(link => link.LinkId); | |
} | |
/* ... */ | |
} |
This file contains 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
public class ABLink | |
{ | |
public int LinkId { get; set; } | |
public int AId { get; set; } | |
public ClassA ClassA { get; set; } | |
public int BId { get; set; } | |
public ClassB ClassB { get; set; } | |
} |
This file contains 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
public class ClassA | |
{ | |
public int AId { get; set; } | |
public string AName { get; set; } | |
// Many to many | |
public List<ClassB> ClassBs { get; set; } | |
} |
This file contains 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
public class ClassB | |
{ | |
public int BId { get; set; } | |
public string BName { get; set; } | |
// Many to many | |
public List<ClassA> ClassAs { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment