Commit e076d595 authored by Steve Smith's avatar Steve Smith

Sqlite working with file in root of web

parent 37942f19
...@@ -5,6 +5,7 @@ using CleanArchitecture.Core.Entities; ...@@ -5,6 +5,7 @@ using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.SharedKernel; using CleanArchitecture.Core.SharedKernel;
using Ardalis.EFCore.Extensions; using Ardalis.EFCore.Extensions;
using System.Reflection; using System.Reflection;
using JetBrains.Annotations;
namespace CleanArchitecture.Infrastructure.Data namespace CleanArchitecture.Infrastructure.Data
{ {
...@@ -12,6 +13,10 @@ namespace CleanArchitecture.Infrastructure.Data ...@@ -12,6 +13,10 @@ namespace CleanArchitecture.Infrastructure.Data
{ {
private readonly IDomainEventDispatcher _dispatcher; private readonly IDomainEventDispatcher _dispatcher;
public AppDbContext(DbContextOptions options) : base(options)
{
}
public AppDbContext(DbContextOptions<AppDbContext> options, IDomainEventDispatcher dispatcher) public AppDbContext(DbContextOptions<AppDbContext> options, IDomainEventDispatcher dispatcher)
: base(options) : base(options)
{ {
...@@ -34,6 +39,9 @@ namespace CleanArchitecture.Infrastructure.Data ...@@ -34,6 +39,9 @@ namespace CleanArchitecture.Infrastructure.Data
{ {
int result = base.SaveChanges(); int result = base.SaveChanges();
// ignore events if no dispatcher provided
if (_dispatcher == null) return result;
// dispatch events only if save was successful // dispatch events only if save was successful
var entitiesWithEvents = ChangeTracker.Entries<BaseEntity>() var entitiesWithEvents = ChangeTracker.Entries<BaseEntity>()
.Select(e => e.Entity) .Select(e => e.Entity)
......
// <auto-generated />
using CleanArchitecture.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace CleanArchitecture.Infrastructure.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20181203222230_InitialModel")]
partial class InitialModel
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024");
modelBuilder.Entity("CleanArchitecture.Core.Entities.ToDoItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Description");
b.Property<bool>("IsDone");
b.Property<string>("Title");
b.HasKey("Id");
b.ToTable("ToDoItems");
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace CleanArchitecture.Infrastructure.Data.Migrations
{
public partial class InitialModel : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ToDoItems",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(nullable: true),
Description = table.Column<string>(nullable: true),
IsDone = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ToDoItems", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ToDoItems");
}
}
}
// <auto-generated />
using CleanArchitecture.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace CleanArchitecture.Infrastructure.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024");
modelBuilder.Entity("CleanArchitecture.Core.Entities.ToDoItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Description");
b.Property<bool>("IsDone");
b.Property<string>("Title");
b.HasKey("Id");
b.ToTable("ToDoItems");
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.AspNetCore; using CleanArchitecture.Infrastructure.Data;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace CleanArchitecture.Web namespace CleanArchitecture.Web
{ {
...@@ -7,7 +12,27 @@ namespace CleanArchitecture.Web ...@@ -7,7 +12,27 @@ namespace CleanArchitecture.Web
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateWebHostBuilder(args).Build().Run(); var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<AppDbContext>();
// context.Database.Migrate();
context.Database.EnsureCreated();
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
} }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
......
using CleanArchitecture.Core.Entities; using CleanArchitecture.Core.Entities;
using CleanArchitecture.Infrastructure.Data; using CleanArchitecture.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace CleanArchitecture.Web namespace CleanArchitecture.Web
{ {
public static class SeedData public static class SeedData
{ {
public static void Initialize(IServiceProvider serviceProvider)
{
using (var dbContext = new AppDbContext(
serviceProvider.GetRequiredService<DbContextOptions<AppDbContext>>()))
{
// Look for any TODO items.
if (dbContext.ToDoItems.Any())
{
return; // DB has been seeded
}
dbContext.ToDoItems.Add(new ToDoItem()
{
Title = "Test Item 1",
Description = "Test Description One"
});
dbContext.ToDoItems.Add(new ToDoItem()
{
Title = "Test Item 2",
Description = "Test Description Two"
});
dbContext.SaveChanges();
}
}
public static void PopulateTestData(AppDbContext dbContext) public static void PopulateTestData(AppDbContext dbContext)
{ {
var toDos = dbContext.ToDoItems; var toDos = dbContext.ToDoItems;
......
...@@ -35,8 +35,12 @@ namespace CleanArchitecture.Web ...@@ -35,8 +35,12 @@ namespace CleanArchitecture.Web
}); });
// TODO: Add DbContext and IOC // TODO: Add DbContext and IOC
string dbName = Guid.NewGuid().ToString(); string dbName = Guid.NewGuid().ToString();
// create schema before using:
// https://www.meziantou.net/2017/09/11/testing-ef-core-in-memory-using-sqlite
services.AddDbContext<AppDbContext>(options => services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase(dbName)); options.UseSqlite("Data Source=database.sqlite"));
// options.UseInMemoryDatabase(dbName));
//options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc() services.AddMvc()
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
<handlers> <handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers> </handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00" /> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00">
<environmentVariables />
</aspNetCore>
</system.webServer> </system.webServer>
</configuration> </configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment