From 3b61dbe8cfd00780c457ae53ca544d242170e7c7 Mon Sep 17 00:00:00 2001 From: baollier1 Date: Wed, 15 Mar 2023 16:31:24 +0100 Subject: [PATCH] add many to many with champions skill --- Sources/EFLol/ChampionEntity.cs | 18 +++++---- Sources/EFLol/MyDbContext.cs | 69 +++++++++++++++++---------------- Sources/EFLol/Program.cs | 36 +++++++++++++++-- Sources/EFLol/SkillEntity.cs | 10 +++-- 4 files changed, 84 insertions(+), 49 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 10e36fe..39c0042 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -11,14 +11,16 @@ namespace EFLol public string Name { get; set; } [MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")] - public string Bio { get; set; } - - //public ChampionClass Class { get; set; } - //public string Icon { get; set; } - //public LargeImage Image { get; set; } - //public ReadOnlyDictionary Characteristics { get; private set; } - - private HashSet skills = new HashSet(); + public string Bio { get; set; } + + //public ChampionClass Class { get; set; } + //public string Icon { get; set; } + //public LargeImage Image { get; set; } + //public ReadOnlyDictionary Characteristics { get; private set; } + + + + public HashSet skills = new HashSet(); public Collection Skins { get; set; } public Collection ListRunePages { get; set; } } diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 55f4036..7416c4d 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -1,43 +1,46 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace EFLol -{ - public class MyDbContext : DbContext - { - public DbSet Champions { get; set; } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace EFLol +{ + public class MyDbContext : DbContext + { + public DbSet Champions { get; set; } public DbSet Skins { get; set; } + public DbSet Skill { get; set; } public DbSet RunePages { get; set; } public DbSet Runes { get; set; } - public MyDbContext() - { } - - public MyDbContext(DbContextOptions options) - : base(options) - { } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (!optionsBuilder.IsConfigured) - { - optionsBuilder.UseSqlite("Data Source=loldb.db"); - } - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); + public MyDbContext() + { } + + public MyDbContext(DbContextOptions options) + : base(options) + { } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlite("Data Source=loldb.db"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().HasMany(p => p.skills).WithMany(p => p.champions).UsingEntity(j => j.ToTable("ChampionSkills")); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); } - } -} + } +} diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index 89b837e..6720439 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; -using System.Collections.ObjectModel; -using EFLol; - - +using System.Collections.ObjectModel; +using EFLol; + + internal class Program { private static void Main(string[] args) @@ -15,10 +15,12 @@ internal class Program context.Database.EnsureCreated(); } + SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; + RuneEntity rune1 = new RuneEntity { Id = 1, Name = "rune1", Description = "super rune1" }; RuneEntity rune2 = new RuneEntity { Id = 2, Name = "rune2", Description = "super rune2" }; RuneEntity rune3 = new RuneEntity { Id = 3, Name = "rune3", Description = "super rune3" }; @@ -32,12 +34,38 @@ internal class Program ListRunePages = new Collection(new List { { runePage1 } }) }; + SkillEntity skill1 = new SkillEntity + { + Id = 1, + Name = "skill1", + Description = "super skill", + SkillType = SkillTypeEntity.Basic, + champions = new HashSet { Zeus, Hera } + }; + + SkillEntity skill2 = new SkillEntity + { + Id = 2, + Name = "skill2", + Description = "super skill", + SkillType = SkillTypeEntity.Basic, + champions = new HashSet { Zeus} + }; + + Zeus.skills.Add(skill1); + Zeus.skills.Add(skill2); + + Hera.skills.Add(skill1); + using (var context = new MyDbContext()) { Console.WriteLine("Creates and inserts new Champions"); context.Add(Zeus); context.Add(Hera); context.Add(Poseidon); + + context.Add(skill1); + context.Add(skill2); context.SaveChanges(); } diff --git a/Sources/EFLol/SkillEntity.cs b/Sources/EFLol/SkillEntity.cs index 1f670d1..c9c90ee 100644 --- a/Sources/EFLol/SkillEntity.cs +++ b/Sources/EFLol/SkillEntity.cs @@ -11,10 +11,12 @@ namespace EFLol public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } - public SkillTypeEntity SkillType { get; set; } - } - - public enum SkillTypeEntity + public SkillTypeEntity SkillType { get; set; } + public HashSet champions { get; set; } + + } + + public enum SkillTypeEntity { Unknown, Basic,