Skip to content

Commit 0293d03

Browse files
committed
fix bug cannot do migration
1 parent 4930936 commit 0293d03

File tree

10 files changed

+146
-75
lines changed

10 files changed

+146
-75
lines changed

samples/ExchangeService/NetCoreKit.Samples.ExchangeService.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Grpc" Version="1.17.1" />
25-
<PackageReference Include="Grpc.HealthCheck" Version="1.17.1" />
26-
<PackageReference Include="Grpc.Tools" Version="1.17.1">
24+
<PackageReference Include="Grpc" Version="1.19.0" />
25+
<PackageReference Include="Grpc.HealthCheck" Version="1.19.0" />
26+
<PackageReference Include="Grpc.Tools" Version="1.19.0">
2727
<PrivateAssets>all</PrivateAssets>
2828
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2929
</PackageReference>

samples/TodoApi/Infrastructure/Db/TodoListDbContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
3+
using NetCoreKit.Domain;
24
using NetCoreKit.Infrastructure.EfCore.Db;
35

46
namespace NetCoreKit.Samples.TodoAPI.Infrastructure.Db
57
{
68
public class TodoListDbContext : AppDbContext
79
{
8-
public TodoListDbContext(DbContextOptions options) : base(options)
10+
public TodoListDbContext(DbContextOptions<TodoListDbContext> options, IConfiguration config, IDomainEventDispatcher eventBus)
11+
: base(options, config, eventBus)
912
{
1013
}
1114
}

samples/TodoApi/Migrations/20181207042249_InitTodoDb.Designer.cs renamed to samples/TodoApi/Migrations/20190406102459_InitTodoDb.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/TodoApi/Migrations/20181207042249_InitTodoDb.cs renamed to samples/TodoApi/Migrations/20190406102459_InitTodoDb.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ public partial class InitTodoDb : Migration
88
protected override void Up(MigrationBuilder migrationBuilder)
99
{
1010
migrationBuilder.CreateTable(
11-
"Projects",
12-
table => new
11+
name: "Projects",
12+
columns: table => new
1313
{
1414
Id = table.Column<Guid>(nullable: false),
1515
Created = table.Column<DateTime>(nullable: false),
1616
Updated = table.Column<DateTime>(nullable: false),
1717
Version = table.Column<int>(nullable: false),
1818
Name = table.Column<string>(nullable: true)
1919
},
20-
constraints: table => { table.PrimaryKey("PK_Projects", x => x.Id); });
20+
constraints: table =>
21+
{
22+
table.PrimaryKey("PK_Projects", x => x.Id);
23+
});
2124

2225
migrationBuilder.CreateTable(
23-
"Tasks",
24-
table => new
26+
name: "Tasks",
27+
columns: table => new
2528
{
2629
Id = table.Column<Guid>(nullable: false),
2730
Created = table.Column<DateTime>(nullable: false),
@@ -37,26 +40,26 @@ protected override void Up(MigrationBuilder migrationBuilder)
3740
{
3841
table.PrimaryKey("PK_Tasks", x => x.Id);
3942
table.ForeignKey(
40-
"FK_Tasks_Projects_ProjectId",
41-
x => x.ProjectId,
42-
"Projects",
43-
"Id",
43+
name: "FK_Tasks_Projects_ProjectId",
44+
column: x => x.ProjectId,
45+
principalTable: "Projects",
46+
principalColumn: "Id",
4447
onDelete: ReferentialAction.Cascade);
4548
});
4649

4750
migrationBuilder.CreateIndex(
48-
"IX_Tasks_ProjectId",
49-
"Tasks",
50-
"ProjectId");
51+
name: "IX_Tasks_ProjectId",
52+
table: "Tasks",
53+
column: "ProjectId");
5154
}
5255

5356
protected override void Down(MigrationBuilder migrationBuilder)
5457
{
5558
migrationBuilder.DropTable(
56-
"Tasks");
59+
name: "Tasks");
5760

5861
migrationBuilder.DropTable(
59-
"Projects");
62+
name: "Projects");
6063
}
6164
}
6265
}

samples/TodoApi/Migrations/TodoListDbContextModelSnapshot.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
// <auto-generated />
2-
32
using System;
43
using Microsoft.EntityFrameworkCore;
54
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
66
using NetCoreKit.Samples.TodoAPI.Infrastructure.Db;
77

88
namespace NetCoreKit.Samples.TodoApi.Migrations
99
{
1010
[DbContext(typeof(TodoListDbContext))]
11-
internal class TodoListDbContextModelSnapshot : ModelSnapshot
11+
partial class TodoListDbContextModelSnapshot : ModelSnapshot
1212
{
1313
protected override void BuildModel(ModelBuilder modelBuilder)
1414
{
1515
#pragma warning disable 612, 618
1616
modelBuilder
17-
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024")
17+
.HasAnnotation("ProductVersion", "2.2.2-servicing-10034")
1818
.HasAnnotation("Relational:MaxIdentifierLength", 64);
1919

2020
modelBuilder.Entity("NetCoreKit.Samples.TodoAPI.Domain.Project", b =>
21-
{
22-
b.Property<Guid>("Id")
23-
.ValueGeneratedOnAdd();
21+
{
22+
b.Property<Guid>("Id")
23+
.ValueGeneratedOnAdd();
2424

25-
b.Property<DateTime>("Created");
25+
b.Property<DateTime>("Created");
2626

27-
b.Property<string>("Name");
27+
b.Property<string>("Name");
2828

29-
b.Property<DateTime>("Updated");
29+
b.Property<DateTime>("Updated");
3030

31-
b.Property<int>("Version");
31+
b.Property<int>("Version");
3232

33-
b.HasKey("Id");
33+
b.HasKey("Id");
3434

35-
b.ToTable("Projects");
36-
});
35+
b.ToTable("Projects");
36+
});
3737

3838
modelBuilder.Entity("NetCoreKit.Samples.TodoAPI.Domain.Task", b =>
39-
{
40-
b.Property<Guid>("Id")
41-
.ValueGeneratedOnAdd();
39+
{
40+
b.Property<Guid>("Id")
41+
.ValueGeneratedOnAdd();
4242

43-
b.Property<Guid>("AuthorId");
43+
b.Property<Guid>("AuthorId");
4444

45-
b.Property<string>("AuthorName");
45+
b.Property<string>("AuthorName");
4646

47-
b.Property<bool?>("Completed");
47+
b.Property<bool?>("Completed");
4848

49-
b.Property<DateTime>("Created");
49+
b.Property<DateTime>("Created");
5050

51-
b.Property<int?>("Order");
51+
b.Property<int?>("Order");
5252

53-
b.Property<Guid>("ProjectId");
53+
b.Property<Guid>("ProjectId");
5454

55-
b.Property<string>("Title")
56-
.IsRequired();
55+
b.Property<string>("Title")
56+
.IsRequired();
5757

58-
b.Property<DateTime>("Updated");
58+
b.Property<DateTime>("Updated");
5959

60-
b.HasKey("Id");
60+
b.HasKey("Id");
6161

62-
b.HasIndex("ProjectId");
62+
b.HasIndex("ProjectId");
6363

64-
b.ToTable("Tasks");
65-
});
64+
b.ToTable("Tasks");
65+
});
6666

6767
modelBuilder.Entity("NetCoreKit.Samples.TodoAPI.Domain.Task", b =>
68-
{
69-
b.HasOne("NetCoreKit.Samples.TodoAPI.Domain.Project", "Project")
70-
.WithMany("Tasks")
71-
.HasForeignKey("ProjectId")
72-
.OnDelete(DeleteBehavior.Cascade);
73-
});
68+
{
69+
b.HasOne("NetCoreKit.Samples.TodoAPI.Domain.Project", "Project")
70+
.WithMany("Tasks")
71+
.HasForeignKey("ProjectId")
72+
.OnDelete(DeleteBehavior.Cascade);
73+
});
7474
#pragma warning restore 612, 618
7575
}
7676
}

samples/TodoApi/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"Features": {
88
"EfCore": {
99
"MySqlDb": {
10-
"FQDN": "127.0.0.1:32605",
10+
"FQDN": "127.0.0.1:3306",
1111
"Password": "P@ssw0rd"
1212
}
1313
},

src/NetCoreKit.Infrastructure.EfCore/Db/AppDbContext.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66
using System.Threading.Tasks;
77
using Humanizer;
88
using Microsoft.EntityFrameworkCore;
9-
using Microsoft.EntityFrameworkCore.Infrastructure;
109
using Microsoft.Extensions.Configuration;
1110
using NetCoreKit.Domain;
1211

1312
namespace NetCoreKit.Infrastructure.EfCore.Db
1413
{
1514
public abstract class AppDbContext : DbContext
1615
{
17-
protected AppDbContext(DbContextOptions options) : base(options)
16+
private readonly IConfiguration _config;
17+
private readonly IDomainEventDispatcher _eventBus = null;
18+
19+
protected AppDbContext(DbContextOptions options, IConfiguration config, IDomainEventDispatcher eventBus = null)
20+
: base(options)
1821
{
22+
_config = config;
23+
_eventBus = eventBus ?? new MemoryDomainEventDispatcher();
1924
}
2025

2126
protected override void OnModelCreating(ModelBuilder builder)
2227
{
23-
var config = this.GetService<IConfiguration>();
24-
2528
var typeToRegisters = new List<Type>();
2629

27-
var ourModules = config.LoadFullAssemblies();
30+
var ourModules = _config.LoadFullAssemblies();
2831

2932
typeToRegisters.AddRange(ourModules.SelectMany(m => m.DefinedTypes));
3033

@@ -39,25 +42,15 @@ protected override void OnModelCreating(ModelBuilder builder)
3942

4043
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
4144
{
42-
var eventBus = this.GetService<IDomainEventDispatcher>();
43-
4445
var result = base.SaveChangesAsync(cancellationToken);
45-
46-
if (eventBus != null)
47-
SaveChangesWithEvents(eventBus);
48-
46+
SaveChangesWithEvents(_eventBus);
4947
return result;
5048
}
5149

5250
public override int SaveChanges()
5351
{
54-
var eventBus = this.GetService<IDomainEventDispatcher>();
55-
5652
var result = base.SaveChanges();
57-
58-
if (eventBus != null)
59-
SaveChangesWithEvents(eventBus);
60-
53+
SaveChangesWithEvents(_eventBus);
6154
return result;
6255
}
6356

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using AutoMapper;
5+
using MediatR;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
10+
using NetCoreKit.Domain;
11+
using NetCoreKit.Infrastructure.Features;
12+
13+
namespace NetCoreKit.Infrastructure.GrpcHost
14+
{
15+
public static class HostBuilderExtensions
16+
{
17+
public static IHost ConfigureDefaultSettings(this HostBuilder hostBuilder,
18+
string[] args,
19+
Action<IServiceCollection> preDbWorkHook = null,
20+
Action<IServiceCollection> moreRegisterAction = null)
21+
{
22+
return hostBuilder
23+
.ConfigureHostConfiguration(configHost =>
24+
{
25+
configHost.SetBasePath(Directory.GetCurrentDirectory());
26+
configHost.AddJsonFile("hostsettings.json", optional: true);
27+
configHost.AddEnvironmentVariables();
28+
configHost.AddCommandLine(args);
29+
})
30+
.ConfigureAppConfiguration((hostContext, configApp) =>
31+
{
32+
configApp.AddJsonFile("appsettings.json", optional: true);
33+
configApp.AddJsonFile(
34+
$"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
35+
optional: true);
36+
configApp.AddEnvironmentVariables();
37+
configApp.AddCommandLine(args);
38+
})
39+
.ConfigureServices((hostContext, services) =>
40+
{
41+
services.AddLogging();
42+
services.AddFeatureToggle();
43+
44+
using (var scope = services.BuildServiceProvider().GetService<IServiceScopeFactory>().CreateScope())
45+
{
46+
var svcProvider = scope.ServiceProvider;
47+
var config = svcProvider.GetRequiredService<IConfiguration>();
48+
var feature = svcProvider.GetRequiredService<IFeature>();
49+
50+
services.AddSingleton<IDomainEventDispatcher, MemoryDomainEventDispatcher>();
51+
52+
preDbWorkHook?.Invoke(services);
53+
54+
Mapper.Initialize(cfg => cfg.AddProfiles(config.LoadFullAssemblies()));
55+
services.AddMediatR(config.LoadFullAssemblies().ToArray());
56+
57+
moreRegisterAction?.Invoke(services);
58+
}
59+
})
60+
.ConfigureLogging((hostContext, configLogging) =>
61+
{
62+
configLogging.AddConsole();
63+
configLogging.AddDebug();
64+
})
65+
.UseConsoleLifetime()
66+
.Build();
67+
}
68+
}
69+
}

src/NetCoreKit.Infrastructure.GrpcHost/NetCoreKit.Infrastructure.GrpcHost.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Grpc" Version="1.17.1" />
20+
<PackageReference Include="AutoMapper" Version="8.0.0" />
21+
<PackageReference Include="Grpc" Version="1.19.0" />
2122
<PackageReference Include="Grpc.Reflection" Version="1.17.1" />
2223
<PackageReference Include="Grpc.Tools" Version="1.17.1">
2324
<PrivateAssets>all</PrivateAssets>
2425
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2526
</PackageReference>
27+
<PackageReference Include="MediatR" Version="6.0.0" />
2628
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
2729
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
2830
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
@@ -39,6 +41,7 @@
3941
</ItemGroup>
4042

4143
<ItemGroup>
44+
<ProjectReference Include="..\NetCoreKit.Infrastructure\NetCoreKit.Infrastructure.csproj" />
4245
<ProjectReference Include="..\NetCoreKit.Utils\NetCoreKit.Utils.csproj" />
4346
</ItemGroup>
4447

templates/NetCoreKit.RestTemplate.EfCore/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IServiceCollection AddEfCoreTemplate<TDbContext>(this IServiceColl
4242
{
4343
if (feature.IsEnabled("Mongo")) throw new Exception("Should turn MongoDb feature off.");
4444

45-
services.AddDbContextPool<TDbContext>((sp, o) =>
45+
services.AddDbContext<TDbContext>((sp, o) =>
4646
{
4747
var extendOptionsBuilder = sp.GetRequiredService<IExtendDbContextOptionsBuilder>();
4848
var connStringFactory = sp.GetRequiredService<IDatabaseConnectionStringFactory>();

0 commit comments

Comments
 (0)