diff --git a/Sources/EF_UT/EntityTest.cs b/Sources/EF_UT/EntityTest.cs index 4122418..5fd0afb 100644 --- a/Sources/EF_UT/EntityTest.cs +++ b/Sources/EF_UT/EntityTest.cs @@ -23,11 +23,12 @@ namespace EF_UT using (var context = new LoLDbContext(options)) { - ChampionEntity chewie = new ChampionEntity("Chewbacca", "", ""); - ChampionEntity yoda = new ChampionEntity("Yoda", "", ""); - ChampionEntity ewok = new ChampionEntity("Ewok", "", ""); - + ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "", Icon = "" }; + ChampionEntity yoda = new ChampionEntity{ Name = "Yoda", Bio = "", Icon = "" }; + ChampionEntity ewok = new ChampionEntity{ Name = "Ewok", Bio = "", Icon = "" }; + //SkinEntity defaulSkin = new SkinEntity("Skin Default", chewie); + //chewie.AddSkin(defaulSkin); Console.WriteLine("Creates and inserts new Champion for tests"); context.Add(chewie); context.Add(yoda); @@ -54,9 +55,9 @@ namespace EF_UT //prepares the database with one instance of the context using (var context = new LoLDbContext(options)) { - ChampionEntity chewie = new ChampionEntity("Chewbacca", "ewa", ""); - ChampionEntity yoda = new ChampionEntity("Yoda", "wewo", ""); - ChampionEntity ewok = new ChampionEntity("Ewok", "", ""); + ChampionEntity chewie = new ChampionEntity{ Name = "Chewbacca", Bio = "ewa", Icon = "" }; + ChampionEntity yoda = new ChampionEntity{ Name = "Yoda", Bio = "wewo", Icon = "" }; + ChampionEntity ewok = new ChampionEntity{ Name = "Ewok", Bio = "", Icon = "" }; context.Add(chewie); context.Add(yoda); diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index d672964..456b9ce 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -32,14 +32,29 @@ namespace EntityFramework //public ImmutableHashSet Skills => skills.ToImmutableHashSet(); //private HashSet skills = new HashSet(); - private ICollection Skills { get; set; } + public ICollection Skills { get; set; } = new Collection(); - public ChampionEntity(string name,string bio,string icon) { - this.Name = name; - this.Bio = bio; - this.Icon = icon; - Skills= new List(); - } + //public ReadOnlyCollection Skins { get; private set; } + //private List skins = new(); + + public ICollection skins { get; set; } = new Collection(); + + //public LargeImageEntity? Image { get; set; } ====> voir pour faire "plus propre" => créé une table pour l'entity Largeimage + public string? Image { get; set; } + + + + /// + /// pas besoin de constructeur ! (sans lui, il est possible d'utiliser la syntaxe utilisé dans le stubbedbDBCOntext) + /// + /// + //public ChampionEntity(string name,string bio,string icon) { + // this.Name = name; + // this.Bio = bio; + // this.Icon = icon; + // Skills= new List(); + // //Skins = new ReadOnlyCollection(skins); + //} public override string ToString() { @@ -53,5 +68,15 @@ namespace EntityFramework public void RemoveSkill(SkillEntity skill) => Skills.Remove(skill); + + public bool AddSkin(SkinEntity skin) + { + if (skins.Contains(skin)) + return false; + skins.Add(skin); + return true; + } + + } } diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs index 7d61851..af9be3b 100644 --- a/Sources/EntityFramework/LoLDbContext.cs +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -11,6 +11,11 @@ namespace EntityFramework { public DbSet Champions { get; set; } + public DbSet Skins { get; set; } + + public DbSet Image { get; set; } + + public LoLDbContext() { } @@ -46,7 +51,25 @@ namespace EntityFramework modelBuilder.Entity().Property(entity => entity.Icon) .IsRequired(); - + + + + /// One to many + /// ChampionEntity 1 ---> * SkinEntity + //création de la table Skin + modelBuilder.Entity().HasKey(skin => skin.Name); //définition de la clé primaire + modelBuilder.Entity().Property(skin => skin.Name) + .ValueGeneratedOnAdd(); //définition du mode de génération de la clé : génération à l'insertion + + // Add the shadow property to the model + modelBuilder.Entity() + .Property("ChampionEntityForeignKey"); + + // Use the shadow property as a foreign key + modelBuilder.Entity() + .HasOne(skin => skin.Champion) + .WithMany(champion => champion.skins) + .HasForeignKey("ChampionEntityForeignKey"); } } diff --git a/Sources/EntityFramework/Manager/EFDataManager.Champions.cs b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs index 8dd70b4..bb87e6f 100644 --- a/Sources/EntityFramework/Manager/EFDataManager.Champions.cs +++ b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs @@ -61,7 +61,7 @@ namespace EntityFramework.Manager using (var context = new LoLDBContextWithStub()) { IEnumerable champ = context.Champions.Where(c => c.Name.Contains(substring)); - return champ. + return champ.ToList().Where(l => l.toCh) } } diff --git a/Sources/EntityFramework/Mapper/ChampionMapper.cs b/Sources/EntityFramework/Mapper/ChampionMapper.cs index 09b48c3..2a3e9f4 100644 --- a/Sources/EntityFramework/Mapper/ChampionMapper.cs +++ b/Sources/EntityFramework/Mapper/ChampionMapper.cs @@ -10,7 +10,7 @@ namespace EntityFramework.Mapper public static class ChampionMapper { public static ChampionEntity ToEntity(this Champion champion) { - return new ChampionEntity(champion.Name, champion.Bio, champion.Icon); + return new ChampionEntity { Name = champion.Name, Bio = champion.Bio, Icon = champion.Icon }; } } diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs index 65ef614..818faa5 100644 --- a/Sources/EntityFramework/Program.cs +++ b/Sources/EntityFramework/Program.cs @@ -1,12 +1,13 @@ // See https://aka.ms/new-console-template for more information using EntityFramework; +using Microsoft.EntityFrameworkCore; -using( var context = new LoLDbContext()) +using ( var context = new LoLDbContext()) { - context.Add(new ChampionEntity("test","test","test") ); + //context.Add(new ChampionEntity{ Name = "test", Bio = "test", Icon = "test" } ); context.SaveChanges(); - ChampionEntity champ = context.Find(1); + ChampionEntity champ = context.Find("Akali"); if( champ != null) { @@ -20,18 +21,58 @@ using( var context = new LoLDbContext()) } //Test BDD Skills - ChampionEntity champSkill = new ChampionEntity("nomSkill", "bioSkill", "iconSkill"); + ChampionEntity champSkill = new ChampionEntity { Name="nomSkill", Bio="bioSkill", Icon="iconSkill" }; - SkillEntity s1 = new SkillEntity("Skill1", "desc", SkillType.Unknown); - SkillEntity s2 = new SkillEntity("Skill2", "desc2", SkillType.Ultimate); - SkillEntity s3 = new SkillEntity("Skill3", "desc3", SkillType.Passive); + //SkillEntity s1 = new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }; + SkillEntity s2 = new SkillEntity { Name="Skill2", Description="desc2", Type=SkillType.Ultimate }; + SkillEntity s3 = new SkillEntity { Name = "Skill3", Description = "desc3", Type = SkillType.Passive }; - champSkill.AddSkill(s1); + champSkill.AddSkill(new SkillEntity { Name = "Skill1", Description = "desc", Type = SkillType.Unknown }); champSkill.AddSkill(s2); champSkill.AddSkill(s3); context.Add(champSkill); - + context.SaveChanges(); + + //OneToMany + Console.WriteLine("Champions : "); + foreach (var champi in context.Champions.Include(a => a.skins)) + { + Console.WriteLine($"\t{champi.Name} : {champi.Bio}"); + foreach (var s in champi.skins) + { + Console.WriteLine($"\t\t{s.Name}"); + } + } + + Console.WriteLine(); + + Console.WriteLine("Skin :"); + foreach (var s in context.Skins) + { + Console.WriteLine($"\t{s.Name}: {s.Description} (Champion : {s.Champion.Name})"); + } + + + Console.WriteLine("\nAjout d'un Champion et 6 Skins...\n"); + + ChampionEntity captainMarvel = new ChampionEntity { Name = "Captain Marvel", Bio="Mais que fait un avenger ici ??", Icon="Icon.png"}; + SkinEntity[] skins = { new SkinEntity {Name = "La Fiesta", Champion = captainMarvel}, + new SkinEntity { Name = "Five Hundred Miles High", Champion = captainMarvel }, + new SkinEntity { Name = "Captain Marvel", Champion = captainMarvel }, + new SkinEntity { Name = "Time's Lie", Champion = captainMarvel }, + new SkinEntity { Name = "Lush Life", Champion = captainMarvel }, + new SkinEntity { Name = "Day Waves", Champion = captainMarvel } + }; + foreach (var s in skins) + { + captainMarvel.skins.Add(s); + } + + context.Add(captainMarvel); + context.SaveChanges(); + + } diff --git a/Sources/EntityFramework/SkillEntity.cs b/Sources/EntityFramework/SkillEntity.cs index d370410..505a427 100644 --- a/Sources/EntityFramework/SkillEntity.cs +++ b/Sources/EntityFramework/SkillEntity.cs @@ -10,42 +10,44 @@ namespace EntityFramework public class SkillEntity { - public SkillType Type { get; private set; } + public SkillType Type { get; set; } [Key] - public string Name - { - get => name; - private init - { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException("a Skill needs a name"); - } - name = value; - } - } - private readonly string name = null!; + public string Name { get; set; } + //public string Name + //{ + // get => name; + // private init + // { + // if (string.IsNullOrWhiteSpace(value)) + // { + // throw new ArgumentException("a Skill needs a name"); + // } + // name = value; + // } + //} + //private readonly string name = null!; - public string Description - { - get => description; - set - { - if (string.IsNullOrWhiteSpace(value)) - { - description = ""; - return; - } - description = value; - } - } - private string description = ""; + public string Description { get; set; } + //public string Description + //{ + // get => description; + // set + // { + // if (string.IsNullOrWhiteSpace(value)) + // { + // description = ""; + // return; + // } + // description = value; + // } + //} + //private string description = ""; - public SkillEntity(string Name, string Description, SkillType Type) { - this.name = Name; - this.Description = Description; - this.Type = Type; - } + //public SkillEntity(string Name, string Description, SkillType Type) { + // this.name = Name; + // this.Description = Description; + // this.Type = Type; + //} } }