Browse Source

feat: ef migrations with sqlite

Mariano Z. 2 năm trước cách đây
mục cha
commit
042dd1d4d4

+ 12 - 0
Products.Business/Domain/User.cs

@@ -0,0 +1,12 @@
+using System.ComponentModel.DataAnnotations;
+using Products.Common.Types;
+
+namespace Products.Business.Domain;
+
+public class User
+{
+    [Required] public required string Name { get; set; }
+    [Required] public Color Color { get; set; }
+    [Required] public required string Brand { get; set; }
+    [Required] public int Id { get; set; }
+}

+ 105 - 0
Products.Business/Migrations/20240323162103_InitialCreate.Designer.cs

@@ -0,0 +1,105 @@
+// <auto-generated />
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Products.Business.Persistence;
+
+#nullable disable
+
+namespace Products.Business.Migrations
+{
+    [DbContext(typeof(DataContext))]
+    [Migration("20240323162103_InitialCreate")]
+    partial class InitialCreate
+    {
+        /// <inheritdoc />
+        protected override void BuildTargetModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder.HasAnnotation("ProductVersion", "7.0.11");
+
+            modelBuilder.Entity("Products.Business.Domain.Product", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Brand")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.Property<int>("Color")
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Products");
+
+                    b.HasData(
+                        new
+                        {
+                            Id = 1,
+                            Brand = "Brand Azul",
+                            Color = 2,
+                            Name = "Product Azul"
+                        },
+                        new
+                        {
+                            Id = 2,
+                            Brand = "Brand Rojo",
+                            Color = 0,
+                            Name = "Product Rojo"
+                        },
+                        new
+                        {
+                            Id = 3,
+                            Brand = "Brand Verde",
+                            Color = 1,
+                            Name = "Product Verde"
+                        },
+                        new
+                        {
+                            Id = 4,
+                            Brand = "Brand Azul #2",
+                            Color = 2,
+                            Name = "Product Azul #2"
+                        },
+                        new
+                        {
+                            Id = 5,
+                            Brand = "Brand Rojo #2",
+                            Color = 0,
+                            Name = "Product Rojo #2"
+                        });
+                });
+
+            modelBuilder.Entity("Products.Business.Domain.User", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Brand")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.Property<int>("Color")
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Users");
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}

+ 68 - 0
Products.Business/Migrations/20240323162103_InitialCreate.cs

@@ -0,0 +1,68 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
+
+namespace Products.Business.Migrations
+{
+    /// <inheritdoc />
+    public partial class InitialCreate : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.CreateTable(
+                name: "Products",
+                columns: table => new
+                {
+                    Id = table.Column<int>(type: "INTEGER", nullable: false)
+                        .Annotation("Sqlite:Autoincrement", true),
+                    Name = table.Column<string>(type: "TEXT", nullable: false),
+                    Color = table.Column<int>(type: "INTEGER", nullable: false),
+                    Brand = table.Column<string>(type: "TEXT", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_Products", x => x.Id);
+                });
+
+            migrationBuilder.CreateTable(
+                name: "Users",
+                columns: table => new
+                {
+                    Id = table.Column<int>(type: "INTEGER", nullable: false)
+                        .Annotation("Sqlite:Autoincrement", true),
+                    Name = table.Column<string>(type: "TEXT", nullable: false),
+                    Color = table.Column<int>(type: "INTEGER", nullable: false),
+                    Brand = table.Column<string>(type: "TEXT", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_Users", x => x.Id);
+                });
+
+            migrationBuilder.InsertData(
+                table: "Products",
+                columns: new[] { "Id", "Brand", "Color", "Name" },
+                values: new object[,]
+                {
+                    { 1, "Brand Azul", 2, "Product Azul" },
+                    { 2, "Brand Rojo", 0, "Product Rojo" },
+                    { 3, "Brand Verde", 1, "Product Verde" },
+                    { 4, "Brand Azul #2", 2, "Product Azul #2" },
+                    { 5, "Brand Rojo #2", 0, "Product Rojo #2" }
+                });
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropTable(
+                name: "Products");
+
+            migrationBuilder.DropTable(
+                name: "Users");
+        }
+    }
+}

+ 102 - 0
Products.Business/Migrations/DataContextModelSnapshot.cs

@@ -0,0 +1,102 @@
+// <auto-generated />
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Products.Business.Persistence;
+
+#nullable disable
+
+namespace Products.Business.Migrations
+{
+    [DbContext(typeof(DataContext))]
+    partial class DataContextModelSnapshot : ModelSnapshot
+    {
+        protected override void BuildModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder.HasAnnotation("ProductVersion", "7.0.11");
+
+            modelBuilder.Entity("Products.Business.Domain.Product", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Brand")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.Property<int>("Color")
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Products");
+
+                    b.HasData(
+                        new
+                        {
+                            Id = 1,
+                            Brand = "Brand Azul",
+                            Color = 2,
+                            Name = "Product Azul"
+                        },
+                        new
+                        {
+                            Id = 2,
+                            Brand = "Brand Rojo",
+                            Color = 0,
+                            Name = "Product Rojo"
+                        },
+                        new
+                        {
+                            Id = 3,
+                            Brand = "Brand Verde",
+                            Color = 1,
+                            Name = "Product Verde"
+                        },
+                        new
+                        {
+                            Id = 4,
+                            Brand = "Brand Azul #2",
+                            Color = 2,
+                            Name = "Product Azul #2"
+                        },
+                        new
+                        {
+                            Id = 5,
+                            Brand = "Brand Rojo #2",
+                            Color = 0,
+                            Name = "Product Rojo #2"
+                        });
+                });
+
+            modelBuilder.Entity("Products.Business.Domain.User", b =>
+                {
+                    b.Property<int>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Brand")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.Property<int>("Color")
+                        .HasColumnType("INTEGER");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("TEXT");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Users");
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}

+ 10 - 8
Products.Business/Persistence/DataContext.cs

@@ -7,18 +7,20 @@ namespace Products.Business.Persistence;
 public sealed class DataContext : DbContext
 {
     public required DbSet<Product> Products { get; set; }
+    public required DbSet<User> Users { get; set; }
+
+    public DataContext() { }
+
+    public DataContext(DbContextOptions<DataContext> options) : base(options) { }
 
     protected override void OnConfiguring
         (DbContextOptionsBuilder optionsBuilder)
     {
-        optionsBuilder.UseInMemoryDatabase("ProductsDB");
-    }
-    
-    public DataContext(DbContextOptions<DataContext> options) : base(options)
-    {
-        Database.EnsureCreated();
+        optionsBuilder.UseSqlite("Data Source=../products.db");
+
     }
 
+
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
         // seed the database with dummy data
@@ -59,7 +61,7 @@ public sealed class DataContext : DbContext
                 Color = Color.Red
             }
         );
-        
+
         base.OnModelCreating(modelBuilder);
     }
-}
+}

+ 5 - 1
Products.Business/Products.Business.csproj

@@ -11,8 +11,12 @@
     </ItemGroup>
 
     <ItemGroup>
+        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
+          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+          <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
         <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.0" />
-        <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.0" />
+        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.0" />
     </ItemGroup>
 
 </Project>

+ 0 - 1
README.md

@@ -101,4 +101,3 @@ Se ha realizado un deploy de la aplicación MVC en Fly.io. Puedes acceder a ella
 
 https://products.mz.uy/
 
-