Skip to content

Commit

Permalink
Updating and testing migrations (dotnet-architecture#42)
Browse files Browse the repository at this point in the history
Updating readme to describe how to run migrations
  • Loading branch information
ardalis authored Aug 22, 2017
1 parent ecb4889 commit eefc817
Show file tree
Hide file tree
Showing 9 changed files with 842 additions and 22 deletions.
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,54 @@ The goal for this sample is to demonstrate some of the principles and patterns d

After cloning or downloading the sample you should be able to run it using an In Memory database immediately.

If you wish to use the sample with a persistent database, you will need to run its Entity Framework Core migrations before you will be able to run the app. To do so, open a command prompt in the Web folder and execute the following commands:
If you wish to use the sample with a persistent database, you will need to run its Entity Framework Core migrations before you will be able to run the app, and update `ConfigureServices` method in `Startup.cs`.

### Configuring the sample to use SQL Server

1. Update `Startup.cs`'s `ConfigureServices` method as follows:

```
public void ConfigureServices(IServiceCollection services)
{
// Requires LocalDB which can be installed with SQL Server Express 2016
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
services.AddDbContext<CatalogContext>(c =>
{
try
{
//c.UseInMemoryDatabase("Catalog");
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
c.ConfigureWarnings(wb =>
{
//By default, in this application, we don't want to have client evaluations
wb.Log(RelationalEventId.QueryClientEvaluationWarning);
});
}
catch (System.Exception ex )
{
var message = ex.Message;
}
});
// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
//options.UseInMemoryDatabase("Identity"));
options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
// leave the rest of the method as-is
```
1. Ensure your connection strings in `appsettings.json` point to a local SQL Server instance.

1. Open a command prompt in the Web folder and execute the following commands:

```
dotnet restore
dotnet ef database update --context Microsoft.eShopWeb.Infrastructure.CatalogContext
dotnet ef database update -c catalogcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj
dotnet ef database update -c appidentitydbcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj
```

**NOTE** The application uses a separate DbContext for its authentication system, which is not yet complete.
These commands will create two separate databases, one for the store's catalog data and shopping cart information, and one for the app's user credentials and identity data.

1. Run the application.
The first time you run the application, it will seed both databases with data such that you should see products in the store, and you should be able to log in using the demouser@microsoft.com account.
4 changes: 4 additions & 0 deletions src/Infrastructure/Data/CatalogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class CatalogContext : DbContext
public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
{
}
//public CatalogContext()
//{
// // required by migrations
//}
public DbSet<Basket> Baskets { get; set; }
public DbSet<CatalogItem> CatalogItems { get; set; }
public DbSet<CatalogBrand> CatalogBrands { get; set; }
Expand Down

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

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Infrastructure.Data.Migrations
{
public partial class Initial : Migration
public partial class InitialModel : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Expand All @@ -20,6 +21,19 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "catalog_type_hilo",
incrementBy: 10);

migrationBuilder.CreateTable(
name: "Baskets",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
BuyerId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Baskets", x => x.Id);
});

migrationBuilder.CreateTable(
name: "CatalogBrand",
columns: table => new
Expand All @@ -44,6 +58,28 @@ protected override void Up(MigrationBuilder migrationBuilder)
table.PrimaryKey("PK_CatalogType", x => x.Id);
});

migrationBuilder.CreateTable(
name: "BasketItem",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
BasketId = table.Column<int>(nullable: true),
CatalogItemId = table.Column<int>(nullable: false),
Quantity = table.Column<int>(nullable: false),
UnitPrice = table.Column<decimal>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BasketItem", x => x.Id);
table.ForeignKey(
name: "FK_BasketItem_Baskets_BasketId",
column: x => x.BasketId,
principalTable: "Baskets",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

migrationBuilder.CreateTable(
name: "Catalog",
columns: table => new
Expand Down Expand Up @@ -73,6 +109,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_BasketItem_BasketId",
table: "BasketItem",
column: "BasketId");

migrationBuilder.CreateIndex(
name: "IX_Catalog_CatalogBrandId",
table: "Catalog",
Expand All @@ -86,9 +127,15 @@ protected override void Up(MigrationBuilder migrationBuilder)

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BasketItem");

migrationBuilder.DropTable(
name: "Catalog");

migrationBuilder.DropTable(
name: "Baskets");

migrationBuilder.DropTable(
name: "CatalogBrand");

Expand Down
58 changes: 50 additions & 8 deletions src/Infrastructure/Data/Migrations/CatalogContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Infrastructure.Data;

namespace Infrastructure.Data.Migrations
{
Expand All @@ -10,13 +13,45 @@ partial class CatalogContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "1.1.0-rtm-22752")
.HasAnnotation("ProductVersion", "1.1.2")
.HasAnnotation("SqlServer:Sequence:.catalog_brand_hilo", "'catalog_brand_hilo', '', '1', '10', '', '', 'Int64', 'False'")
.HasAnnotation("SqlServer:Sequence:.catalog_hilo", "'catalog_hilo', '', '1', '10', '', '', 'Int64', 'False'")
.HasAnnotation("SqlServer:Sequence:.catalog_type_hilo", "'catalog_type_hilo', '', '1', '10', '', '', 'Int64', 'False'")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("eShopWeb.Models.CatalogBrand", b =>
modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.Basket", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("BuyerId");

b.HasKey("Id");

b.ToTable("Baskets");
});

modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.BasketItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int?>("BasketId");

b.Property<int>("CatalogItemId");

b.Property<int>("Quantity");

b.Property<decimal>("UnitPrice");

b.HasKey("Id");

b.HasIndex("BasketId");

b.ToTable("BasketItem");
});

modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogBrand", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand All @@ -32,7 +67,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("CatalogBrand");
});

modelBuilder.Entity("eShopWeb.Models.CatalogItem", b =>
modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand Down Expand Up @@ -62,7 +97,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("Catalog");
});

modelBuilder.Entity("eShopWeb.Models.CatalogType", b =>
modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand All @@ -78,14 +113,21 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("CatalogType");
});

modelBuilder.Entity("eShopWeb.Models.CatalogItem", b =>
modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.BasketItem", b =>
{
b.HasOne("Microsoft.eShopWeb.ApplicationCore.Entities.Basket")
.WithMany("Items")
.HasForeignKey("BasketId");
});

modelBuilder.Entity("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogItem", b =>
{
b.HasOne("eShopWeb.Models.CatalogBrand", "CatalogBrand")
b.HasOne("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogBrand", "CatalogBrand")
.WithMany()
.HasForeignKey("CatalogBrandId")
.OnDelete(DeleteBehavior.Cascade);

b.HasOne("eShopWeb.Models.CatalogType", "CatalogType")
b.HasOne("Microsoft.eShopWeb.ApplicationCore.Entities.CatalogType", "CatalogType")
.WithMany()
.HasForeignKey("CatalogTypeId")
.OnDelete(DeleteBehavior.Cascade);
Expand Down
Loading

0 comments on commit eefc817

Please sign in to comment.