Skip to content

Commit

Permalink
移除efcore.sharding使用shardingcore
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Sep 28, 2022
1 parent c036b16 commit ce1117a
Show file tree
Hide file tree
Showing 41 changed files with 905 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<PackageReference Include="AspNetCore.HealthChecks.UI.MySql.Storage" Version="6.0.5" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.9" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="EFCore.Sharding.MySql" Version="6.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using EFCore.Sharding;
using IoTSharp;
using IoTSharp.Contracts;
using IoTSharp.Data;
Expand All @@ -8,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;
using ShardingCore.Core.ShardingConfigurations;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -38,10 +38,16 @@ public static void ConfigureMySql(this IServiceCollection services, string co
healthChecksUI.AddMySqlStorage(connectionString);

}
public static void UseMySqlToSharding(this IShardingBuilder builder, string connectionString, ShardingByDateMode expandBy)
public static void UseMySqlToSharding(this ShardingConfigOptions options)
{
builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.MySql);
builder.SetDateSharding<TelemetryData>(nameof(TelemetryData.DateTime), (ExpandByDateMode)(int)expandBy, DateTime.Now);
options.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseMySql(conn, new MySqlServerVersion(new Version()));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Oracle" Version="6.0.3" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="6.0.5" />
<PackageReference Include="EFCore.Sharding.Oracle" Version="6.0.7" />
<PackageReference Include="Oracle.EntityFrameworkCore" Version="6.21.61" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

using EFCore.Sharding;
using IoTSharp;
using IoTSharp.Contracts;
using IoTSharp.Data;
using IoTSharp.Data.Oracle;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using ShardingCore.Core.ShardingConfigurations;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -29,10 +29,16 @@ public static void ConfigureOracle(this IServiceCollection services, string conn

}

public static void UseOracleToSharding(this IShardingBuilder builder, string connectionString, ShardingByDateMode expandBy)
public static void UseOracleToSharding(this ShardingConfigOptions options)
{
builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.Oracle);
builder.SetDateSharding<TelemetryData>(nameof(TelemetryData.DateTime), (ExpandByDateMode)(int)expandBy, DateTime.Now);
options.UseShardingQuery((conStr, builder) =>
{
builder.UseOracle(conStr);
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseOracle(conn);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="6.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.PostgreSQL.Storage" Version="6.0.4" />
<PackageReference Include="EFCore.Sharding.PostgreSql" Version="6.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

using EFCore.Sharding;
using IoTSharp;
using IoTSharp.Contracts;
using IoTSharp.Data;
using IoTSharp.Data.PostgreSQL;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using ShardingCore.Core.ShardingConfigurations;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -29,10 +29,16 @@ public static void ConfigureNpgsql(this IServiceCollection services, string conn

}

public static void UseNpgsqlToSharding(this IShardingBuilder builder, string connectionString, ShardingByDateMode expandBy)
public static void UseNpgsqlToSharding(this ShardingConfigOptions options)
{
builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.PostgreSql);
builder.SetDateSharding<TelemetryData>(nameof(TelemetryData.DateTime),(ExpandByDateMode)(int)expandBy, DateTime.Now);
options.UseShardingQuery((conStr, builder) =>
{
builder.UseNpgsql(conStr);
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseNpgsql(conn);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.SqlServer.Storage" Version="6.0.5" />
<PackageReference Include="EFCore.Sharding.SqlServer" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

using EFCore.Sharding;
using IoTSharp;
using IoTSharp.Contracts;
using IoTSharp.Data;
using IoTSharp.Data.SqlServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using ShardingCore.Core.ShardingConfigurations;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -29,10 +29,16 @@ public static void ConfigureSqlServer(this IServiceCollection services, string c

}

public static void UseSqlServerToSharding(this IShardingBuilder builder, string connectionString, ShardingByDateMode expandBy)
public static void UseSqlServerToSharding(this ShardingConfigOptions options)
{
builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.SqlServer);
builder.SetDateSharding<TelemetryData>(nameof(TelemetryData.DateTime), (ExpandByDateMode)(int)expandBy, DateTime.Now);
options.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlServer(conStr);
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseSqlServer(conn);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<PackageReference Include="AspNetCore.HealthChecks.UI.Sqlite.Storage" Version="6.0.5" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Core" Version="6.0.5" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.9" />
<PackageReference Include="EFCore.Sharding.SQLite" Version="6.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\IoTSharp.Data\IoTSharp.Data.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using EFCore.Sharding;
using IoTSharp;
using IoTSharp.Contracts;
using IoTSharp.Data;
Expand All @@ -8,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using ShardingCore.Core.ShardingConfigurations;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -30,10 +30,16 @@ public static void ConfigureSqlite(this IServiceCollection services, string conn

}

public static void UseSQLiteToSharding(this IShardingBuilder builder, string connectionString, ShardingByDateMode expandBy)
public static void UseSQLiteToSharding(this ShardingConfigOptions options)
{
builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.SQLite);
builder.SetDateSharding<TelemetryData>(nameof(TelemetryData.DateTime), (ExpandByDateMode)(int)expandBy, DateTime.Now);
options.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlite(conStr);
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseSqlite(conn);
});
}

public static void SetCaseInsensitiveSearchesForSQLite(this ModelBuilder modelBuilder)
Expand Down
2 changes: 1 addition & 1 deletion IoTSharp.Data.TimeSeries/IoTSharp.Data.TimeSeries.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<PackageReference Include="Apache.IoTDB.Data" Version="0.13.0.8" />

<PackageReference Include="EFCore.Sharding" Version="6.0.7" />
<!-- <PackageReference Include="EFCore.Sharding" Version="6.0.7" />-->

<PackageReference Include="hyjiacan.pinyin4net" Version="4.1.1" />

Expand Down
14 changes: 7 additions & 7 deletions IoTSharp.Data.TimeSeries/ShardingStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EFCore.Sharding;
using IoTSharp.Contracts;
using IoTSharp.Contracts;
using IoTSharp.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,6 +9,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using IoTSharp.Data.Shardings;

namespace IoTSharp.Storage
{
Expand Down Expand Up @@ -92,10 +92,10 @@ public Task<List<TelemetryDataDto>> LoadTelemetryAsync(Guid deviceId, string key
{
using (var scope = _scopeFactor.CreateScope())
{
using (var context = scope.ServiceProvider.GetService<IShardingDbAccessor>())
using (var context = scope.ServiceProvider.GetRequiredService<ShardingDbContext>())
{
var lst = new List<TelemetryDataDto>();
var kv = context.GetIShardingQueryable<TelemetryData>()
var kv = context.Set<TelemetryData>()
.Where(t => t.DeviceId == deviceId && t.DateTime >= begin && t.DateTime < end)
.ToList().Select(t => new TelemetryDataDto() { DateTime = t.DateTime, KeyName = t.KeyName, Value = t.ToObject() });
if (!string.IsNullOrEmpty(keys))
Expand Down Expand Up @@ -128,7 +128,7 @@ public Task<List<TelemetryDataDto>> LoadTelemetryAsync(Guid deviceId, string key
{
using var scope = _scopeFactor.CreateScope();

using (var db = scope.ServiceProvider.GetService<IShardingDbAccessor>())
using (var db = scope.ServiceProvider.GetRequiredService<ShardingDbContext>())
{
var lst = new List<TelemetryData>();
msg.MsgBody.ToList().ForEach(kp =>
Expand All @@ -141,8 +141,8 @@ public Task<List<TelemetryDataDto>> LoadTelemetryAsync(Guid deviceId, string key
telemetries.Add(tdata);
}
});
int ret = await db.InsertAsync(lst);
_logger.LogInformation($"新增({msg.DeviceId})遥测数据{ret}");
await db.AddAsync(lst);
_logger.LogInformation($"新增({msg.DeviceId})遥测数据1");
}
}
catch (Exception ex)
Expand Down
7 changes: 2 additions & 5 deletions IoTSharp.Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
using Microsoft.Extensions.DependencyInjection;
using IoTSharp.Data.Configurations;
using IoTSharp.Contracts;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding.Abstractions;

namespace IoTSharp.Data
{
public class ApplicationDbContext : IdentityDbContext
{


public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Expand Down Expand Up @@ -46,9 +46,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
}




public DbSet<Tenant> Tenant { get; set; }
public DbSet<Customer> Customer { get; set; }
public DbSet<Relationship> Relationship { get; set; }
Expand Down
1 change: 1 addition & 0 deletions IoTSharp.Data/IoTSharp.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="ShardingCore" Version="6.6.0.36" />

</ItemGroup>

Expand Down
Loading

0 comments on commit ce1117a

Please sign in to comment.