-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for many-to-many relationship configuration.
Fixes #1978
- Loading branch information
1 parent
eead0aa
commit 1ec45ea
Showing
9 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EFModeling.FluentAPI | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
|
||
} | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
samples/core/Modeling/FluentAPI/Relationships/ManyToManyPayload.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace EFModeling.FluentAPI.Relationships.ManyToManyPayload | ||
{ | ||
#region ManyToManyPayload | ||
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; } | ||
} | ||
#endregion | ||
} |
72 changes: 72 additions & 0 deletions
72
samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace EFModeling.FluentAPI.Relationships.ManyToManyShared | ||
{ | ||
public 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) | ||
{ | ||
#region SharedConfiguration | ||
modelBuilder | ||
.Entity<Post>() | ||
.HasMany(p => p.Tags) | ||
.WithMany(p => p.Posts) | ||
.UsingEntity(j => j.ToTable("PostTags")); | ||
#endregion | ||
|
||
#region Metadata | ||
foreach (var entityType in modelBuilder.Model.GetEntityTypes()) | ||
{ | ||
foreach (var skipNavigation in entityType.GetSkipNavigations()) | ||
{ | ||
Console.WriteLine(entityType.DisplayName() + "." + skipNavigation.Name); | ||
} | ||
} | ||
#endregion | ||
|
||
#region Seeding | ||
modelBuilder | ||
.Entity<Post>() | ||
.HasData(new Post { PostId = 1, Title = "First"}); | ||
|
||
modelBuilder | ||
.Entity<Tag>() | ||
.HasData(new Tag { TagId = "ef" }); | ||
|
||
modelBuilder | ||
.Entity<Post>() | ||
.HasMany(p => p.Tags) | ||
.WithMany(p => p.Posts) | ||
.UsingEntity(j => j.HasData(new { PostsPostId = 1, TagsTagId = "ef" })); | ||
#endregion | ||
} | ||
} | ||
|
||
#region ManyToManyShared | ||
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 class Tag | ||
{ | ||
public string TagId { get; set; } | ||
|
||
public ICollection<Post> Posts { get; set; } | ||
} | ||
#endregion | ||
} |