EF2
nathan boileau 2 years ago
parent e2a3c3e463
commit 2419cb1e9f

@ -18,6 +18,6 @@ namespace EFLol
//public LargeImage Image { get; set; }
//public ReadOnlyDictionary<string, int> Characteristics { get; private set; }
//private HashSet<Skill> skills = new HashSet<Skill>();
public ReadOnlyCollection<SkinEntity> Skins { get; set; }
public Collection<SkinEntity> Skins { get; set; }
}
}

@ -11,6 +11,7 @@ namespace EFLol
{
public DbSet<ChampionEntity> Champions { get; set; }
public DbSet<SkinEntity> Skins { get; set; }
public DbSet<RuneEntity> Runes { get; set; }
public MyDbContext()
{ }
@ -32,6 +33,7 @@ namespace EFLol
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ChampionEntity>().Property(c => c.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<SkinEntity>().Property(s => s.Id).ValueGeneratedOnAdd();
}
}
}

@ -18,14 +18,14 @@ ChampionEntity Zeus = new ChampionEntity
{
Name = "Zeus",
Bio = "Zeus is the god of the sky",
Skins = new ReadOnlyCollection<SkinEntity>(new List<SkinEntity> { black, white })
Skins = new Collection<SkinEntity>(new List<SkinEntity> { black, white })
};
ChampionEntity Hera = new ChampionEntity
{
Name = "Hera",
Bio = "Hera is the goddess of marriage",
Skins = new ReadOnlyCollection<SkinEntity>(new List<SkinEntity> { green })
Skins = new Collection<SkinEntity>(new List<SkinEntity> { green })
};
ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" };
@ -34,7 +34,6 @@ ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseido
using (var context = new MyDbContext())
{
// Crée des champions et les insère dans la base
Console.WriteLine("Creates and inserts new Champions");
context.Add(Zeus);
context.Add(Hera);
@ -47,12 +46,11 @@ using (var context = new MyDbContext())
{
foreach (var n in context.Champions)
{
// Use LINQ to display the skins for each champion
var skins = from s in context.Skins
where n.Id == s.Id
select s;
// LINQ to select the skins of the current champion
var skins = context.Champions.Where(c => c.Id == n.Id).SelectMany(c => c.Skins);
Console.WriteLine($"Champion n°{n.Id} - {n.Name}");
// Display the champion and its skins
Console.WriteLine($"Champion n°{n.Id} - {n.Name} : {skins.Count()} skins");
}
context.SaveChanges();
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFLol
{
public class RuneEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
//public RuneFamily Family { get; set; }
//public string Icon { get; set; }
//public string Image { get; set; }
}
}
Loading…
Cancel
Save