📦 Création du OneToMany mais probleme dans la création de la migration

pull/23/head
Pierre Ferreira 2 years ago
parent c72adccc1a
commit 11137d89aa

@ -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<Skill> Skills => skills.ToImmutableHashSet();
//private HashSet<Skill> skills = new HashSet<Skill>();
private ICollection<SkillEntity> Skills { get; set; }
public ICollection<SkillEntity> Skills { get; set; }
public ReadOnlyCollection<SkinEntity> Skins { get; private set; }
private List<SkinEntity> skins = new();
//public ReadOnlyCollection<SkinEntity> Skins { get; private set; }
//private List<SkinEntity> skins = new();
public LargeImageEntity Image { get; set; }
public ICollection<SkinEntity> 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<SkillEntity>();
Skins = new ReadOnlyCollection<SkinEntity>(skins);
}
/// <summary>
/// pas besoin de constructeur ! (sans lui, il est possible d'utiliser la syntaxe utilisé dans le stubbedbDBCOntext)
/// </summary>
/// <returns></returns>
//public ChampionEntity(string name,string bio,string icon) {
// this.Name = name;
// this.Bio = bio;
// this.Icon = icon;
// Skills= new List<SkillEntity>();
// //Skins = new ReadOnlyCollection<SkinEntity>(skins);
//}
public override string ToString()
{

@ -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;
//}
}
}

@ -11,6 +11,8 @@ namespace EntityFramework
{
public DbSet<ChampionEntity> Champions { get; set; }
public DbSet<SkinEntity> Skins { get; set; }
public LoLDbContext()
{ }
@ -46,7 +48,25 @@ namespace EntityFramework
modelBuilder.Entity<ChampionEntity>().Property(entity => entity.Icon)
.IsRequired();
/// One to many
/// ChampionEntity 1 ---> * SkinEntity
//création de la table Skin
modelBuilder.Entity<SkinEntity>().HasKey(skin => skin.Name); //définition de la clé primaire
modelBuilder.Entity<SkinEntity>().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<SkinEntity>()
.Property<int>("ChampionEntityForeignKey");
// Use the shadow property as a foreign key
modelBuilder.Entity<SkinEntity>()
.HasOne(skin => skin.Champion)
.WithMany(champion => champion.skins)
.HasForeignKey("ChampionEntityForeignKey");
}
}

@ -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<ChampionEntity>(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();
}

@ -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;
//}
}
}

@ -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<ChampionEntity>().HasData(corichard, pintrand);
modelBuilder.Entity<SkinEntity>().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 }
);
}
}
}
Loading…
Cancel
Save