From 11137d89aaae25fad50356141257f38e7385b108 Mon Sep 17 00:00:00 2001
From: Pierre Ferreira
Date: Sat, 11 Mar 2023 11:39:21 +0100
Subject: [PATCH] =?UTF-8?q?:package:=20Cr=C3=A9ation=20du=20OneToMany=20ma?=
=?UTF-8?q?is=20probleme=20dans=20la=20cr=C3=A9ation=20de=20la=20migration?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Sources/EntityFramework/ChampionEntity.cs | 31 +++---
Sources/EntityFramework/LargeImageEntity.cs | 14 ++-
Sources/EntityFramework/LoLDbContext.cs | 22 +++-
Sources/EntityFramework/Program.cs | 49 ++++++++-
Sources/EntityFramework/SkinEntity.cs | 105 +++++++++++---------
Sources/EntityFramework/StubbedContext.cs | 36 +++++++
6 files changed, 186 insertions(+), 71 deletions(-)
create mode 100644 Sources/EntityFramework/StubbedContext.cs
diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs
index 8822711..8452958 100644
--- a/Sources/EntityFramework/ChampionEntity.cs
+++ b/Sources/EntityFramework/ChampionEntity.cs
@@ -15,7 +15,7 @@ namespace EntityFramework
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
- public int Id { get; set; }
+ public int Id { get; set; } = Guid.NewGuid().GetHashCode();
[Required]
[MaxLength(50)]
@@ -32,21 +32,28 @@ namespace EntityFramework
//public ImmutableHashSet Skills => skills.ToImmutableHashSet();
//private HashSet skills = new HashSet();
- private ICollection Skills { get; set; }
+ public ICollection Skills { get; set; }
- public ReadOnlyCollection Skins { get; private set; }
- private List skins = new();
+ //public ReadOnlyCollection Skins { get; private set; }
+ //private List skins = new();
- public LargeImageEntity Image { get; set; }
+ public ICollection skins { get; set; }
+ public LargeImageEntity Image { get; set; } = new LargeImageEntity();
- public ChampionEntity(string name,string bio,string icon) {
- this.Name = name;
- this.Bio = bio;
- this.Icon = icon;
- Skills= new List();
- Skins = new ReadOnlyCollection(skins);
- }
+
+
+ ///
+ /// 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()
{
diff --git a/Sources/EntityFramework/LargeImageEntity.cs b/Sources/EntityFramework/LargeImageEntity.cs
index a1f8ae1..f86eade 100644
--- a/Sources/EntityFramework/LargeImageEntity.cs
+++ b/Sources/EntityFramework/LargeImageEntity.cs
@@ -1,18 +1,22 @@
-using System;
+using Microsoft.EntityFrameworkCore;
+using System;
using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFramework
{
+
public class LargeImageEntity
{
+ [Key]
public string Base64 { get; set; }
- public LargeImageEntity(string base64)
- {
- Base64 = base64;
- }
+ //public LargeImageEntity(string base64)
+ //{
+ // Base64 = base64;
+ //}
}
}
diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs
index 9fa1567..7c8ea74 100644
--- a/Sources/EntityFramework/LoLDbContext.cs
+++ b/Sources/EntityFramework/LoLDbContext.cs
@@ -11,6 +11,8 @@ namespace EntityFramework
{
public DbSet Champions { get; set; }
+ public DbSet Skins { get; set; }
+
public LoLDbContext()
{ }
@@ -46,7 +48,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/Program.cs b/Sources/EntityFramework/Program.cs
index 65ef614..b06d523 100644
--- a/Sources/EntityFramework/Program.cs
+++ b/Sources/EntityFramework/Program.cs
@@ -1,9 +1,10 @@
// 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);
@@ -20,7 +21,7 @@ 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);
@@ -31,7 +32,47 @@ using( var context = new LoLDbContext())
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.Id}: {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/SkinEntity.cs b/Sources/EntityFramework/SkinEntity.cs
index adcf35c..6035674 100644
--- a/Sources/EntityFramework/SkinEntity.cs
+++ b/Sources/EntityFramework/SkinEntity.cs
@@ -6,63 +6,70 @@ using System.Threading.Tasks;
namespace EntityFramework
{
- public class SkinEntity //ON TO MANY
+ public class SkinEntity //ONE TO MANY
{
- public string Name
- {
- get => name;
- private init
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException("A skin must have 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? Name { get; set; }
+
+ public string? Description { get; set; }
+
+ //public string Name
+ //{
+ // get => name;
+ // private init
+ // {
+ // if (string.IsNullOrWhiteSpace(value))
+ // {
+ // throw new ArgumentException("A skin must have 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 Icon { get; set; }
public LargeImageEntity Image { get; set; }
public float Price { get; set; }
+ public ChampionEntity Champion { get; set; }
+
- public ChampionEntity Champion
- {
- get => champion;
- private init
- {
- if (value == null)
- throw new ArgumentNullException("A skill can't have a null champion");
- champion = value;
- }
- }
- private readonly ChampionEntity champion = null!;
+ //public ChampionEntity Champion
+ //{
+ // get => champion;
+ // private init
+ // {
+ // if (value == null)
+ // throw new ArgumentNullException("A skill can't have a null champion");
+ // champion = value;
+ // }
+ //}
+ //private readonly ChampionEntity champion = null!;
- public SkinEntity(string name, ChampionEntity champion, float price = 0.0f, string icon = "", string image = "", string description = "")
- {
- Name = name;
- Champion = champion;
- //Champion.AddSkin(this);
- Price = price;
- Icon = icon;
- Image = new LargeImageEntity(image);
- Description = description;
- }
+ //public SkinEntity(string name, ChampionEntity champion, float price = 0.0f, string icon = "", string image = "", string description = "")
+ //{
+ // Name = name;
+ // Champion = champion;
+ // //Champion.AddSkin(this);
+ // Price = price;
+ // Icon = icon;
+ // Image = new LargeImageEntity(image);
+ // Description = description;
+ //}
}
}
diff --git a/Sources/EntityFramework/StubbedContext.cs b/Sources/EntityFramework/StubbedContext.cs
new file mode 100644
index 0000000..53efd24
--- /dev/null
+++ b/Sources/EntityFramework/StubbedContext.cs
@@ -0,0 +1,36 @@
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EntityFramework
+{
+ public class StubbedContext : LoLDbContext
+ {
+ protected override void OnModelCreating(ModelBuilder modelBuilder)
+ {
+ base.OnModelCreating(modelBuilder);
+
+ ChampionEntity corichard = new ChampionEntity() {Name="Corichard", Bio="biobiobiobio", Icon="/a/a/a/a"};
+ ChampionEntity pintrand = new ChampionEntity { Name = "Pintrand", Bio = "mimimimimim", Icon = "/small.png" };
+
+ modelBuilder.Entity().HasData(corichard, pintrand);
+
+ modelBuilder.Entity().HasData(new { Name = "aaaa", ChampionEntityForeignKey = 1, Description = "So What", Icon="/Icon.png", Price=10.0 },
+ new { Name = "skin", ChampionEntityForeignKey = 1, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "bo", ChampionEntityForeignKey = 1, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "Joulie", ChampionEntityForeignKey = 1, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "Radiance", ChampionEntityForeignKey = 1, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "void", ChampionEntityForeignKey = 1, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "Radiance", ChampionEntityForeignKey = 2, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "void", ChampionEntityForeignKey = 2, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "DarkTheme", ChampionEntityForeignKey = 2, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "gold", ChampionEntityForeignKey = 2, Description = "So What", Icon = "/Icon.png", Price = 10.0 },
+ new { Name = "ruby", ChampionEntityForeignKey = 2, Description = "So What", Icon = "/Icon.png", Price = 10.0 }
+ );
+ }
+
+ }
+}