Skip to content

Commit

Permalink
去掉了跟EF和Include但查询和多查询分割的警告信息
Browse files Browse the repository at this point in the history
  • Loading branch information
maikebing committed Feb 28, 2023
1 parent 5acf901 commit 0cb20a9
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using IoTSharp.Data;
using IoTSharp.Data.InMemory;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;

namespace Microsoft.Extensions.DependencyInjection
Expand All @@ -19,7 +20,7 @@ public static void ConfigureInMemory(this IServiceCollection services,int poolSi
builder.UseInMemoryDatabase("IoTSharp");
builder.UseInternalServiceProvider(services.BuildServiceProvider());
} , poolSize);
healthChecksUI.AddInMemoryStorage();
healthChecksUI.AddInMemoryStorage(opt => opt.ConfigureWarnings(w => w.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
using System;
using System.Diagnostics;
using ShardingCore.Core.ShardingConfigurations;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Microsoft.Extensions.DependencyInjection
{
public static class IoTSharpDataBuilderExtensions
{
public static void ConfigureMySql(this IServiceCollection services, string connectionString,int poolSize , IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
{
services.AddEntityFrameworkMySql();
services.AddSingleton<IDataBaseModelBuilderOptions>(c => new MySqlModelBuilderOptions());
ServerVersion serverVersion=null;
try
Expand All @@ -27,26 +27,24 @@ public static void ConfigureMySql(this IServiceCollection services, string co
throw new Exception($"Can't detect MySql server's version , {ex.Message} ", ex);
}
services.AddSingleton(serverVersion);
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseInternalServiceProvider(services.BuildServiceProvider());
builder.UseMySql(connectionString, serverVersion, s => s.MigrationsAssembly("IoTSharp.Data.MySQL").UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
}
, poolSize );
services.AddMySql<ApplicationDbContext>(connectionString, serverVersion, s =>
s.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
.MigrationsAssembly("IoTSharp.Data.MySQL"));


checksBuilder.AddMySql(connectionString, "IoTSharp.Data.MySQL");
healthChecksUI.AddMySqlStorage(connectionString);
healthChecksUI.AddMySqlStorage(connectionString, opt => opt.ConfigureWarnings(w => w.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)));

}
public static void UseMySqlToSharding(this ShardingConfigOptions options)
{
options.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
builder.UseMySql(conStr, new MySqlServerVersion(new Version()),opt=> opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseMySql(conn, new MySqlServerVersion(new Version()));
builder.UseMySql(conn, new MySqlServerVersion(new Version()), opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using ShardingCore.Core.ShardingConfigurations;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -16,28 +17,24 @@ public static class IoTSharpDataBuilderExtensions

public static void ConfigureOracle(this IServiceCollection services, string connectionString, int poolSize, IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
{
services.AddEntityFrameworkOracle();
services.AddSingleton<IDataBaseModelBuilderOptions>( c=> new OracleModelBuilderOptions());
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseOracle(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.Oracle").UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
builder.UseInternalServiceProvider(services.BuildServiceProvider());
}
, poolSize);
services.AddOracle<ApplicationDbContext>(connectionString, s =>
s.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
.MigrationsAssembly("IoTSharp.Data.Oracle"));
checksBuilder.AddOracle(connectionString,name: "IoTSharp.Data.Oracle");
healthChecksUI.AddInMemoryStorage();
healthChecksUI.AddInMemoryStorage(opt => opt.ConfigureWarnings(w => w.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)));

}

public static void UseOracleToSharding(this ShardingConfigOptions options)
{
options.UseShardingQuery((conStr, builder) =>
{
builder.UseOracle(conStr);
builder.UseOracle(conStr, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseOracle(conn);
builder.UseOracle(conn, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using ShardingCore.Core.ShardingConfigurations;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -16,28 +17,24 @@ public static class IoTSharpDataBuilderExtensions

public static void ConfigureNpgsql(this IServiceCollection services, string connectionString, int poolSize, IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
{
services.AddEntityFrameworkNpgsql();
services.AddSingleton<IDataBaseModelBuilderOptions>( c=> new NpgsqlModelBuilderOptions());
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseNpgsql(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.PostgreSQL").UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
builder.UseInternalServiceProvider(services.BuildServiceProvider());
}
, poolSize);
services.AddNpgsql<ApplicationDbContext>(connectionString, s =>
s.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
.MigrationsAssembly("IoTSharp.Data.PostgreSQL"));
checksBuilder.AddNpgSql(connectionString, name: "IoTSharp.Data.PostgreSQL");
healthChecksUI.AddPostgreSqlStorage(connectionString);
healthChecksUI.AddPostgreSqlStorage(connectionString, opt => opt.ConfigureWarnings(w => w.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)));

}

public static void UseNpgsqlToSharding(this ShardingConfigOptions options)
{
options.UseShardingQuery((conStr, builder) =>
{
builder.UseNpgsql(conStr);
builder.UseNpgsql(conStr, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseNpgsql(conn);
builder.UseNpgsql(conn, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using ShardingCore.Core.ShardingConfigurations;
using Microsoft.Data.Sqlite;
using System.IO;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -23,29 +25,23 @@ public static void ConfigureSqlite(this IServiceCollection services, string conn
var fi = new FileInfo(builder.DataSource);
if (!fi.Directory.Exists) fi.Directory.Create();

services.AddEntityFrameworkSqlite();
services.AddSingleton<IDataBaseModelBuilderOptions>( c=> new SqliteModelBuilderOptions());
services.AddDbContextPool<ApplicationDbContext>(builder =>
{

builder.UseSqlite(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.Sqlite").UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
builder.UseInternalServiceProvider(services.BuildServiceProvider());
}
, poolSize);
services.AddSingleton<IDataBaseModelBuilderOptions>(c => new SqliteModelBuilderOptions());
services.AddSqlite<ApplicationDbContext>(connectionString, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
.MigrationsAssembly("IoTSharp.Data.Sqlite"));
checksBuilder.AddSqlite(connectionString, name: "IoTSharp.Data.Sqlite");
healthChecksUI.AddSqliteStorage($"Data Source={fi.DirectoryName}{Path.DirectorySeparatorChar}health_checks.db");
healthChecksUI.AddSqliteStorage($"Data Source={fi.DirectoryName}{Path.DirectorySeparatorChar}health_checks.db",opt => opt.ConfigureWarnings(w => w.Ignore(RelationalEventId.MultipleCollectionIncludeWarning)));

}

public static void UseSQLiteToSharding(this ShardingConfigOptions options)
{
options.UseShardingQuery((conStr, builder) =>
{
builder.UseSqlite(conStr);
builder.UseSqlite(conStr, opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
options.UseShardingTransaction((conn, builder) =>
{
builder.UseSqlite(conn);
builder.UseSqlite(conn,opt => opt.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
}

Expand Down
10 changes: 5 additions & 5 deletions IoTSharp.Data/DataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public static (bool ok, Device device) GetDeviceByTokenWithTenantCustomer(this A
{
var deviceIdentity = from id in _context.DeviceIdentities.Include(di => di.Device) where id.IdentityId == access_token && id.IdentityType == IdentityType.AccessToken select id;
var devices = from dev in _context.Device.Include(g=>g.Customer).Include(g=>g.Tenant) where deviceIdentity.Any() && dev.Id == deviceIdentity.FirstOrDefault().Device.Id select dev;
bool ok = deviceIdentity == null || !devices.AsSplitQuery().Any();
return (ok, devices.AsSplitQuery().FirstOrDefault());
bool ok = deviceIdentity == null || !devices.Any();
return (ok, devices.FirstOrDefault());
}
public static (bool ok, Device device) GetDeviceByToken(this ApplicationDbContext _context, string access_token)
{
var deviceIdentity = from id in _context.DeviceIdentities.Include(di => di.Device).AsSingleQuery() where id.IdentityId == access_token && id.IdentityType == IdentityType.AccessToken select id;
var deviceIdentity = from id in _context.DeviceIdentities.Include(di => di.Device) where id.IdentityId == access_token && id.IdentityType == IdentityType.AccessToken select id;
var devices = from dev in _context.Device where deviceIdentity.Any() && dev.Id == deviceIdentity.FirstOrDefault().Device.Id select dev;
bool ok = deviceIdentity == null || !devices.AsSplitQuery().Any();
return (ok, devices.AsSplitQuery().FirstOrDefault());
bool ok = deviceIdentity == null || !devices.Any();
return (ok, devices.FirstOrDefault());
}
/// <summary>
/// Save Data to Device's and <typeparamref name="L"/>
Expand Down
6 changes: 3 additions & 3 deletions IoTSharp.Data/Extensions/AlarmExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public static async Task<ApiResult<Alarm>> OccurredAlarm(this ApplicationDbConte
OriginatorType originatorType = cad.OriginatorType;
if (cad.OriginatorType == OriginatorType.Device || cad.OriginatorType == OriginatorType.Gateway || cad.OriginatorType == OriginatorType.Unknow)
{
var dev = _context.Device.Include(d=>d.Tenant).Include(d=>d.Customer).AsSplitQuery().FirstOrDefault(d => d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName);
var dev = _context.Device.Include(d=>d.Tenant).Include(d=>d.Customer).FirstOrDefault(d => d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName);
if (dev != null)
{
if (dev.DeviceType == DeviceType.Gateway)
{
if (dev.Id.ToString() != cad.OriginatorName && dev.Name != cad.OriginatorName)
{
var subdev = from g in _context.Device.Include(d => d.Tenant).Include(d => d.Customer).Include(g => g.Owner).AsSplitQuery() where g.Owner == dev && g.Name == cad.OriginatorName select g;
var subdev = from g in _context.Device.Include(d => d.Tenant).Include(d => d.Customer).Include(g => g.Owner) where g.Owner == dev && g.Name == cad.OriginatorName select g;
var orig = await subdev.FirstOrDefaultAsync();
OriginatorId = orig.Id;
originatorType = OriginatorType.Device;
Expand Down Expand Up @@ -51,7 +51,7 @@ public static async Task<ApiResult<Alarm>> OccurredAlarm(this ApplicationDbConte
}
else if (cad.OriginatorType == OriginatorType.Asset)
{
var ass = _context.Assets.Include(a => a.Tenant).Include(a => a.Customer).AsSplitQuery().FirstOrDefault(d => (d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName) && d.Deleted==false);
var ass = _context.Assets.Include(a => a.Tenant).Include(a => a.Customer).FirstOrDefault(d => (d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName) && d.Deleted==false);
if (ass != null)
{
originatorType = OriginatorType.Asset;
Expand Down
2 changes: 1 addition & 1 deletion IoTSharp.Data/Extensions/DeviceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void AfterCreateDevice(this ApplicationDbContext _context, Device
public static async Task<DeviceRule[]> GerDeviceRulesList(this ApplicationDbContext _dbContext, Guid devid, EventType mountType)
{
DeviceRule[] lst = null;
var r = from dr in _dbContext.DeviceRules.Include(d => d.Device).Include(d => d.FlowRule).AsSplitQuery() where dr.Device.Id == devid && dr.FlowRule.MountType == mountType select dr ;
var r = from dr in _dbContext.DeviceRules.Include(d => d.Device).Include(d => d.FlowRule) where dr.Device.Id == devid && dr.FlowRule.MountType == mountType select dr ;
if (r.Any())
{
lst = await r.ToArrayAsync();
Expand Down
2 changes: 1 addition & 1 deletion IoTSharp/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public async Task<ApiResult<LoginResult>> Register([FromBody] RegisterDto model)
{
await _signInManager.SignInAsync(user, false);
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.Email));
var customer = await _context.Customer.Include(c => c.Tenant).AsSingleQuery().FirstOrDefaultAsync(c => c.Email == model.Customer);
var customer = await _context.Customer.Include(c => c.Tenant).FirstOrDefaultAsync(c => c.Email == model.Customer);
if (customer != null)
{
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.Email));
Expand Down
14 changes: 7 additions & 7 deletions IoTSharp/Controllers/AssetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<ApiResult<PagedData<AssetDto>>> List([FromQuery] QueryDto m)
public async Task<ApiResult<PagedData<AssetRelation>>> AssetRelations(Guid assetid)
{
var data =new PagedData<AssetRelation>();
var result = await _context.Assets.Include(c => c.OwnedAssets).AsSplitQuery().SingleOrDefaultAsync(c => c.Id == assetid);
var result = await _context.Assets.Include(c => c.OwnedAssets).SingleOrDefaultAsync(c => c.Id == assetid);
data.rows = result.OwnedAssets;
data.total = result.OwnedAssets.Count;
return new ApiResult<PagedData<AssetRelation>>(ApiCode.Success, "OK", data);
Expand All @@ -78,7 +78,7 @@ public ApiResult<PagedData<AssetDeviceItem>> Relations(Guid assetid)

var profile = this.GetUserProfile();

var result = _context.Assets.Include(c => c.OwnedAssets).AsSingleQuery()
var result = _context.Assets.Include(c => c.OwnedAssets)
.SingleOrDefault(x =>
x.Id == assetid && x.Customer.Id == profile.Customer && x.Tenant.Id == profile.Tenant && x.Deleted==false)?.OwnedAssets
.ToList().GroupBy(c => c.DeviceId).Select(c => new
Expand Down Expand Up @@ -121,7 +121,7 @@ public ApiResult<PagedData<AssetDeviceItem>> Relations(Guid assetid)
public async Task<ApiResult<AssetDto>> Get(Guid id)
{
var profile = this.GetUserProfile();
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant).AsSingleQuery().SingleOrDefaultAsync(c =>
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant).SingleOrDefaultAsync(c =>
c.Id == id && c.Customer.Id == profile.Customer && c.Tenant.Id == profile.Tenant && c.Deleted==false);
if (asset != null)
{
Expand All @@ -148,7 +148,7 @@ public async Task<ApiResult<bool>> Update([FromBody] AssetDto dto)
{

var profile = this.GetUserProfile();
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant).AsSingleQuery().SingleOrDefaultAsync(c =>
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant).SingleOrDefaultAsync(c =>
c.Id == dto.Id && c.Customer.Id == profile.Customer && c.Tenant.Id == profile.Tenant && c.Deleted==false);
if (asset == null)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public async Task<ApiResult<bool>> Delete(Guid id)
try
{
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant)
.Include(c => c.OwnedAssets).AsSingleQuery().SingleOrDefaultAsync(c =>
.Include(c => c.OwnedAssets).SingleOrDefaultAsync(c =>
c.Id == id && c.Customer.Id == profile.Customer && c.Tenant.Id == profile.Tenant && c.Deleted==false);
if (asset == null)
{
Expand Down Expand Up @@ -246,7 +246,7 @@ public async Task<ApiResult<bool>> addDevice(ModelAddAssetDevice m)
try
{
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant)
.Include(c => c.OwnedAssets).AsSingleQuery().SingleOrDefaultAsync(c =>
.Include(c => c.OwnedAssets).SingleOrDefaultAsync(c =>
c.Id == m.AssetId && c.Customer.Id == profile.Customer && c.Tenant.Id == profile.Tenant && c.Deleted==false);
if (asset == null)
{
Expand Down Expand Up @@ -308,7 +308,7 @@ public async Task<ApiResult<bool>> RemoveDevice(ModelAssetDevice m)
try
{
var asset = await _context.Assets.Include(c => c.Customer).Include(c => c.Tenant)
.Include(c => c.OwnedAssets).AsSingleQuery().SingleOrDefaultAsync(c =>
.Include(c => c.OwnedAssets).SingleOrDefaultAsync(c =>
c.Id == m.AssetId && c.Customer.Id == profile.Customer && c.Tenant.Id == profile.Tenant && c.Deleted==false);

await _context.SaveChangesAsync();
Expand Down
Loading

0 comments on commit 0cb20a9

Please sign in to comment.