Created
January 24, 2019 18:36
-
-
Save mason-mcglothlin/e23abd02fbb62f32caab10961d0d98e3 to your computer and use it in GitHub Desktop.
Stack Overflow Question 54352309
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
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Metadata.Builders; | |
using System; | |
using System.Collections.Generic; | |
namespace HelloConsoleCore | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>() | |
.UseInMemoryDatabase("testdb"); | |
using (var context = new MyDbContext(optionsBuilder.Options)) | |
{ | |
var gameMtmCollection = new Game_Mtm_Collection { }; | |
var game = new Game { Id = Guid.NewGuid(), Name = "Test Game" }; | |
var anotherGameMtmCollection = new Game_Mtm_Collection(); | |
var collection = new Collection { Id = Guid.NewGuid(), Name = "Test Collection", Game_Mtm_Collections = new List<Game_Mtm_Collection> { gameMtmCollection, anotherGameMtmCollection } }; | |
gameMtmCollection.CollectionId = collection.Id; | |
gameMtmCollection.Collection = collection; | |
gameMtmCollection.GameId = game.Id; | |
gameMtmCollection.Game = game; | |
context.Add(collection); | |
context.SaveChanges(); | |
} | |
Console.WriteLine("Press any key to exit"); | |
Console.ReadKey(); | |
} | |
} | |
public class MyDbContext : DbContext | |
{ | |
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
base.OnModelCreating(builder); | |
EntityTypeBuilder<Game> game = builder.Entity<Game>(); | |
EntityTypeBuilder<Collection> collection = builder.Entity<Collection>(); | |
EntityTypeBuilder<Game_Mtm_Collection> game_Mtm_Collection = builder.Entity<Game_Mtm_Collection>(); | |
game_Mtm_Collection.HasKey(_ => new { _.GameId, _.CollectionId }); | |
game_Mtm_Collection.HasOne(_ => _.Game) | |
.WithMany(_ => _.Game_Mtm_Collections) | |
.HasForeignKey(_ => _.GameId); | |
game_Mtm_Collection.HasOne(_ => _.Collection) | |
.WithMany(_ => _.Game_Mtm_Collections) | |
.HasForeignKey(_ => _.CollectionId); | |
} | |
} | |
public class Game | |
{ | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public List<Game_Mtm_Collection> Game_Mtm_Collections { get; set; } | |
} | |
public class Collection | |
{ | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public List<Game_Mtm_Collection> Game_Mtm_Collections { get; set; } | |
} | |
public class Game_Mtm_Collection | |
{ | |
public Guid GameId { get; set; } | |
public Game Game { get; set; } | |
public Guid CollectionId { get; set; } | |
public Collection Collection { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment