Skip to content

Commit

Permalink
数据库迁移分表
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldairarrow committed Sep 25, 2020
1 parent eb6037c commit 3e84506
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 279 deletions.
4 changes: 2 additions & 2 deletions examples/Demo.DateSharding/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Program
static async Task Main(string[] args)
{
DateTime startTime = DateTime.Now.AddMinutes(-5);
await Host.CreateDefaultBuilder()
var host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
//配置初始化
Expand All @@ -23,7 +23,7 @@ await Host.CreateDefaultBuilder()
//按分钟分表
config.SetDateSharding<Base_UnitTest>(nameof(Base_UnitTest.CreateTime), ExpandByDateMode.PerMinute, startTime);
});
}).RunConsoleAsync();
}).Build();
//host.Start();

//var serviceProvider = host.Services;
Expand Down
13 changes: 13 additions & 0 deletions examples/Demo.DbMigrator/CustomContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using EFCore.Sharding;

namespace DbMigrator
{
public class CustomContext : GenericDbContext
{
public CustomContext(GenericDbContext dbContext)
: base(dbContext)
{

}
}
}
23 changes: 0 additions & 23 deletions examples/Demo.DbMigrator/DbContext1.cs

This file was deleted.

23 changes: 0 additions & 23 deletions examples/Demo.DbMigrator/DbContext2.cs

This file was deleted.

2 changes: 2 additions & 0 deletions examples/Demo.DbMigrator/Demo.DbMigrator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.8" />
</ItemGroup>

<ItemGroup>
Expand Down
33 changes: 29 additions & 4 deletions examples/Demo.DbMigrator/DesignTimeDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
using Microsoft.EntityFrameworkCore;
using EFCore.Sharding;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace DbMigrator
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<DbContext>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CustomContext>, IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
throw new System.NotImplementedException();
}

/// <summary>
/// 创建数据库上下文
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public DbContext CreateDbContext(string[] args)
public CustomContext CreateDbContext(string[] args)
{
return new DbContext1();
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddEFCoreSharding(x =>
{
x.MigrationsWithoutForeignKey();
});
}).Build();
host.Start();

var db = host.Services
.GetService<IDbFactory>()
.GetDbContext(new DbContextParamters
{
ConnectionString = "Data Source=localhost;Initial Catalog=DbMigrator;Integrated Security=True",
DbType = DatabaseType.SqlServer,
});

return new CustomContext(db);
}
}
}
2 changes: 2 additions & 0 deletions examples/Demo.DbMigrator/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Demo.DbMigrator
{
[Table(nameof(Order))]
public class Order
{
[Key]
Expand Down

This file was deleted.

This file was deleted.

66 changes: 0 additions & 66 deletions examples/Demo.DbMigrator/Migrations/DbContext1ModelSnapshot.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/EFCore.Sharding/Config/Bootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Hosting;
using EFCore.Sharding.Config;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
Expand All @@ -14,6 +15,7 @@ public Bootstrapper(IServiceProvider serviceProvider, IOptions<EFCoreShardingOpt
{
_serviceProvider = serviceProvider;
_shardingOptions = shardingOptions.Value;
Cache.ServiceProvider = serviceProvider;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Sharding/Config/Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace EFCore.Sharding.Config
{
internal static class Cache
{
public static IServiceProvider ServiceProvider { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/EFCore.Sharding/Config/EFCoreShardingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public static class EFCoreShardingExtensions
/// <param name="services">服务集合</param>
/// <param name="shardingBuilder">配置项</param>
/// <returns></returns>
public static IServiceCollection AddEFCoreSharding(this IServiceCollection services, Action<IShardingBuilder> shardingBuilder)
public static IServiceCollection AddEFCoreSharding(this IServiceCollection services, Action<IShardingBuilder> shardingBuilder = null)
{
services.AddOptions<EFCoreShardingOptions>();
services.AddLogging();

ShardingContainer container = new ShardingContainer(services);
shardingBuilder(container);
shardingBuilder?.Invoke(container);

services.AddSingleton<IShardingBuilder>(container);
services.AddSingleton<IShardingConfig>(container);
Expand Down
Loading

0 comments on commit 3e84506

Please sign in to comment.