add many to many with champions skill

EF2
baollier1 2 years ago
parent e7f6d20133
commit 3b61dbe8cf

@ -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<string, int> Characteristics { get; private set; }
private HashSet<SkillEntity> skills = new HashSet<SkillEntity>();
public string Bio { get; set; }
//public ChampionClass Class { get; set; }
//public string Icon { get; set; }
//public LargeImage Image { get; set; }
//public ReadOnlyDictionary<string, int> Characteristics { get; private set; }
public HashSet<SkillEntity> skills = new HashSet<SkillEntity>();
public Collection<SkinEntity> Skins { get; set; }
public Collection<RunePageEntity> ListRunePages { get; set; }
}

@ -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<ChampionEntity> 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<ChampionEntity> Champions { get; set; }
public DbSet<SkinEntity> Skins { get; set; }
public DbSet<SkillEntity> Skill { get; set; }
public DbSet<RunePageEntity> RunePages { get; set; }
public DbSet<RuneEntity> Runes { get; set; }
public MyDbContext()
{ }
public MyDbContext(DbContextOptions<MyDbContext> 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<ChampionEntity>().Property(c => c.Id).ValueGeneratedOnAdd();
public MyDbContext()
{ }
public MyDbContext(DbContextOptions<MyDbContext> 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<ChampionEntity>().Property(c => c.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<SkinEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<SkillEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<ChampionEntity>().HasMany(p => p.skills).WithMany(p => p.champions).UsingEntity(j => j.ToTable("ChampionSkills"));
modelBuilder.Entity<RunePageEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<RuneEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
}
}
}
}
}

@ -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<RunePageEntity>(new List<RunePageEntity> { { runePage1 } })
};
SkillEntity skill1 = new SkillEntity
{
Id = 1,
Name = "skill1",
Description = "super skill",
SkillType = SkillTypeEntity.Basic,
champions = new HashSet<ChampionEntity> { Zeus, Hera }
};
SkillEntity skill2 = new SkillEntity
{
Id = 2,
Name = "skill2",
Description = "super skill",
SkillType = SkillTypeEntity.Basic,
champions = new HashSet<ChampionEntity> { 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();
}

@ -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<ChampionEntity> champions { get; set; }
}
public enum SkillTypeEntity
{
Unknown,
Basic,

Loading…
Cancel
Save