Skip to content

Commit

Permalink
Add Forecast Summary and DbContext design time
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Sep 28, 2023
1 parent f0fccaa commit 7ef3dc1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 12 deletions.
17 changes: 17 additions & 0 deletions BlazorApp1.Data/WeatherForecastSummary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BlazorApp1.Domain;

public class WeatherForecastSummary
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)Math.Round(TemperatureC / 0.5556, 0);
}
15 changes: 15 additions & 0 deletions BlazorApp1/Shared/Design/WeatherDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace BlazorApp1.Data.Design;

public class WeatherDbContextFactory : IDesignTimeDbContextFactory<WeatherDbContext>
{
public WeatherDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<WeatherDbContext>();
optionsBuilder.UseSqlite("Data Source=Local.sqlite");

return new WeatherDbContext(optionsBuilder.Options);
}
}
102 changes: 102 additions & 0 deletions BlazorApp1/Shared/Migrations/20230928075811_Summaries.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions BlazorApp1/Shared/Migrations/20230928075811_Summaries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BlazorApp1.Shared.Migrations
{
/// <inheritdoc />
public partial class Summaries : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ForecastSummaries",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Date = table.Column<DateOnly>(type: "TEXT", nullable: false),
TemperatureC = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ForecastSummaries", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_ForecastSummaries_Date",
table: "ForecastSummaries",
column: "Date",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ForecastSummaries");
}
}
}
24 changes: 22 additions & 2 deletions BlazorApp1/Shared/Migrations/WeatherDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.11");

modelBuilder.Entity("BlazorApp1.Shared.WeatherForecast", b =>
modelBuilder.Entity("BlazorApp1.Domain.WeatherForecast", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand All @@ -34,7 +34,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.ToTable("Forecast", (string)null);
b.ToTable("Forecast");

b.HasData(
new
Expand Down Expand Up @@ -73,6 +73,26 @@ protected override void BuildModel(ModelBuilder modelBuilder)
TemperatureC = 9
});
});

modelBuilder.Entity("BlazorApp1.Domain.WeatherForecastSummary", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<DateOnly>("Date")
.HasColumnType("TEXT");

b.Property<int>("TemperatureC")
.HasColumnType("INTEGER");

b.HasKey("Id");

b.HasIndex("Date")
.IsUnique();

b.ToTable("ForecastSummaries");
});
#pragma warning restore 612, 618
}
}
Expand Down
25 changes: 15 additions & 10 deletions BlazorApp1/Shared/WeatherDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlite("Data Source=Local.sqlite");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WeatherForecast>().HasData(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
modelBuilder.Entity<WeatherForecast>().HasData(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Id = index,
Date = new DateTime(2022, 1, 1).AddDays(index),
TemperatureC = 4 + index,
Summary = Summaries[1 + index]
})
.ToArray());
}
Id = index,
Date = new DateTime(2022, 1, 1).AddDays(index),
TemperatureC = 4 + index,
Summary = Summaries[1 + index]
})
.ToArray());

modelBuilder.Entity<WeatherForecastSummary>()
.HasIndex(x => x.Date).IsUnique();
}

public DbSet<WeatherForecast> Forecast { get; set; }

public DbSet<WeatherForecastSummary> ForecastSummaries { get; set; }

}

0 comments on commit 7ef3dc1

Please sign in to comment.