push EF database

rune
Louison PARANT 2 years ago
parent 14cec510d4
commit 6650ce89e0

@ -5,11 +5,18 @@ namespace EntityFrameworkLOL.DBContexts
{ {
class SQLiteContext : DbContext class SQLiteContext : DbContext
{ {
/*public DbSet<ChampionEntity> Champion { get; set; } public DbSet<ChampionEntity> Champion { get; set; }
public DbSet<SkinEntity> Skin { get; set; }*/ public DbSet<SkinEntity> Skin { get; set; }
public DbSet<RuneEntity> Rune { get; set; } public DbSet<RuneEntity> Rune { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source=DBLOL.db"); => options.UseSqlite($"Data Source=DBLOL.db");
public SQLiteContext()
{ }
public SQLiteContext(DbContextOptions<SQLiteContext> options)
: base(options)
{ }
} }
} }

@ -12,10 +12,13 @@ namespace EntityFrameworkLOL.Entities
class ChampionEntity class ChampionEntity
{ {
[Key] [Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Bio { get; set; } public string Bio { get; set; }
public string Icon { get; set; } /*public string Icon { get; set; }*/
} }
} }

@ -11,6 +11,9 @@ namespace EntityFrameworkLOL.Entities
class RuneEntity class RuneEntity
{ {
[Key] [Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }

@ -11,6 +11,9 @@ namespace EntityFrameworkLOL.Entities
class SkinEntity class SkinEntity
{ {
[Key] [Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }

@ -1,38 +0,0 @@
// <auto-generated />
using EntityFrameworkLOL.DBContexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
[DbContext(typeof(SQLiteContext))]
[Migration("20230202105714_MigrationWallah2")]
partial class MigrationWallah2
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b =>
{
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Rune");
});
#pragma warning restore 612, 618
}
}
}

@ -1,33 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
/// <inheritdoc />
public partial class MigrationWallah2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Rune",
columns: table => new
{
Name = table.Column<string>(type: "TEXT", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Rune", x => x.Name);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Rune");
}
}
}

@ -0,0 +1,81 @@
// <auto-generated />
using EntityFrameworkLOL.DBContexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
[DbContext(typeof(SQLiteContext))]
[Migration("20230204102144_MigrationWallah3")]
partial class MigrationWallah3
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champion");
});
modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Rune");
});
modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Skin");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,69 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFrameworkLOL.Migrations
{
/// <inheritdoc />
public partial class MigrationWallah3 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champion",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Bio = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champion", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Rune",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Rune", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Skin",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Skin", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champion");
migrationBuilder.DropTable(
name: "Rune");
migrationBuilder.DropTable(
name: "Skin");
}
}
}

@ -16,19 +16,62 @@ namespace EntityFrameworkLOL.Migrations
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b => modelBuilder.Entity("EntityFrameworkLOL.Entities.ChampionEntity", b =>
{ {
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champion");
});
modelBuilder.Entity("EntityFrameworkLOL.Entities.RuneEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description") b.Property<string>("Description")
.IsRequired() .IsRequired()
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.HasKey("Name"); b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Rune"); b.ToTable("Rune");
}); });
modelBuilder.Entity("EntityFrameworkLOL.Entities.SkinEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Skin");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

@ -8,12 +8,12 @@ class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
ChampionEntity akali = new ChampionEntity { Name = "Akali" }; /*ChampionEntity akali = new ChampionEntity { Id = 1, Name = "Akali", Bio = "" };
ChampionEntity aatrox = new ChampionEntity { Name = "Aatrox" }; ChampionEntity aatrox = new ChampionEntity { Id = 2, Name = "Aatrox", Bio = "" };
ChampionEntity ahri = new ChampionEntity { Name = "Ahri" }; ChampionEntity ahri = new ChampionEntity { Id = 3, Name = "Ahri", Bio = "" };
ChampionEntity bard = new ChampionEntity { Name = "Bard" }; ChampionEntity bard = new ChampionEntity { Id = 4, Name = "Bard", Bio = "" };
ChampionEntity alistar = new ChampionEntity { Name = "Alistar" }; ChampionEntity alistar = new ChampionEntity { Id = 5, Name = "Alistar", Bio = "" };
ChampionEntity akshan = new ChampionEntity { Name = "Akshan" }; ChampionEntity akshan = new ChampionEntity { Id = 6, Name = "Akshan", Bio = "" };
using (var context = new ChampionContext()) using (var context = new ChampionContext())
{ {
@ -30,15 +30,15 @@ class Program
var aChampion = context.Champion var aChampion = context.Champion
.Where(c => c.Name.StartsWith("A")) .Where(c => c.Name.StartsWith("A"))
.First(); .First();
Console.WriteLine($"{aChampion.Name} (with bio : {aChampion.Bio}"); Console.WriteLine($"{aChampion.Name} (with bio : \"{aChampion.Bio}\")");
} }
RuneEntity conqueror = new RuneEntity { Name = "Conqueror", Description = "" }; RuneEntity conqueror = new RuneEntity { Id = 1, Name = "Conqueror", Description = "" };
RuneEntity thriumph = new RuneEntity { Name = "Thriumph", Description = "" }; RuneEntity thriumph = new RuneEntity { Id = 2, Name = "Thriumph", Description = "" };
RuneEntity alacrity = new RuneEntity { Name = "Legend : Alacrity", Description = "" }; RuneEntity alacrity = new RuneEntity { Id = 3, Name = "Legend : Alacrity", Description = "" };
RuneEntity tenacity = new RuneEntity { Name = "Legend : Tenacity", Description = "" }; RuneEntity tenacity = new RuneEntity { Id = 4, Name = "Legend : Tenacity", Description = "" };
RuneEntity laststand = new RuneEntity { Name = "Last Stand", Description = "" }; RuneEntity laststand = new RuneEntity { Id = 5, Name = "Last Stand", Description = "" };
RuneEntity laststand2 = new RuneEntity { Name = "Last Stand 2", Description = "" }; RuneEntity laststand2 = new RuneEntity { Id = 6, Name = "Last Stand 2", Description = "" };
using (var context = new RuneContext()) using (var context = new RuneContext())
{ {
@ -55,15 +55,15 @@ class Program
var lRune = context.Rune var lRune = context.Rune
.Where(r => r.Name.StartsWith("L")) .Where(r => r.Name.StartsWith("L"))
.First(); .First();
Console.WriteLine($"{lRune.Name} (with Description : {lRune.Description}"); Console.WriteLine($"{lRune.Name} (with Description : \"{lRune.Description}\")");
} }
SkinEntity stinger = new SkinEntity { Name = "Stinger", Description = "" }; SkinEntity stinger = new SkinEntity { Id = 1, Name = "Stinger", Description = "" };
SkinEntity infernal = new SkinEntity { Name = "Infernal", Description = "" }; SkinEntity infernal = new SkinEntity { Id = 2, Name = "Infernal", Description = "" };
SkinEntity allStar = new SkinEntity { Name = "All-Star", Description = "" }; SkinEntity allStar = new SkinEntity { Id = 3, Name = "All-Star", Description = "" };
SkinEntity justicar = new SkinEntity { Name = "Justicar", Description = "" }; SkinEntity justicar = new SkinEntity { Id = 4, Name = "Justicar", Description = "" };
SkinEntity mecha = new SkinEntity { Name = "Mecha", Description = "" }; SkinEntity mecha = new SkinEntity { Id = 5, Name = "Mecha", Description = "" };
SkinEntity seaHunter = new SkinEntity { Name = "Sea Hunter", Description = "" }; SkinEntity seaHunter = new SkinEntity { Id = 6, Name = "Sea Hunter", Description = "" };
using (var context = new SkinContext()) using (var context = new SkinContext())
{ {
@ -80,18 +80,28 @@ class Program
var iSkin = context.Skin var iSkin = context.Skin
.Where(s => s.Name.StartsWith("I")) .Where(s => s.Name.StartsWith("I"))
.First(); .First();
Console.WriteLine($"{iSkin.Name} (with Description : {iSkin.Description}"); Console.WriteLine($"{iSkin.Name} (with Description : \"{iSkin.Description}\")");
Console.WriteLine("Updates the name of the Infernal Skin"); Console.WriteLine("Updates the name of the Infernal Skin");
iSkin.Name = "Infernal of Hell (Wallah)"; iSkin.Description = "Hella Infernal (Wallah)";
context.SaveChanges(); context.SaveChanges();
Console.WriteLine($"{iSkin.Name} (with Description : {iSkin.Description}"); Console.WriteLine($"{iSkin.Name} (with Description : \"{iSkin.Description}\")");
Console.WriteLine("Deletes one item from the database"); Console.WriteLine("Deletes one item from the database");
var droid = context.Skin var droid = context.Skin
.SingleOrDefault(s => s.Name.Equals("Infernal")); .SingleOrDefault(s => s.Name.Equals("Infernal"));
context.Remove(droid); context.Remove(droid);
context.SaveChanges(); context.SaveChanges();
}*/
using (var context = new ChampionContext())
{
foreach (var c in context.Champion)
{
c.Bio = $"{c.Name}";
Console.WriteLine($"{c.Name} - {c.Bio}");
}
context.SaveChanges();
} }
} }
} }
Loading…
Cancel
Save