Skip to content

Commit 6f31e7e

Browse files
committed
Remedy my reckless autoformatting
1 parent 20f33c7 commit 6f31e7e

File tree

8 files changed

+78
-93
lines changed

8 files changed

+78
-93
lines changed

EntityFramework.Exceptions.Common/ExceptionProcessorInterceptor.cs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace EntityFramework.Exceptions.Common;
1212

13-
public abstract class ExceptionProcessorInterceptor<TProviderException> : IDbCommandInterceptor, ISaveChangesInterceptor
14-
where TProviderException : DbException
13+
public abstract class ExceptionProcessorInterceptor<TProviderException> : IDbCommandInterceptor, ISaveChangesInterceptor where TProviderException : DbException
1514
{
1615
private List<IndexDetails> uniqueIndexDetailsList;
1716
private List<ForeignKeyDetails> foreignKeyDetailsList;
@@ -80,18 +79,15 @@ private void ProcessException(Exception eventException, DbContext eventContext)
8079
throw exception;
8180
}
8281

83-
private void SetConstraintDetails(DbContext context, UniqueConstraintException exception,
84-
TProviderException providerException)
82+
private void SetConstraintDetails(DbContext context, UniqueConstraintException exception, TProviderException providerException)
8583
{
8684
if (uniqueIndexDetailsList == null)
8785
{
88-
var indexes = context.Model.GetEntityTypes()
89-
.SelectMany(x => x.GetDeclaredIndexes().Where(index => index.IsUnique));
90-
91-
var mappedIndexes = indexes.SelectMany(index => index.GetMappedTableIndexes(),
92-
(index, tableIndex) =>
93-
new IndexDetails(tableIndex.Name, tableIndex.Table.SchemaQualifiedName, index.Properties));
86+
var indexes = context.Model.GetEntityTypes().SelectMany(x => x.GetDeclaredIndexes().Where(index => index.IsUnique));
9487

88+
var mappedIndexes = indexes.SelectMany(index => index.GetMappedTableIndexes(),
89+
(index, tableIndex) => new IndexDetails(tableIndex.Name, tableIndex.Table.SchemaQualifiedName, index.Properties));
90+
9591
var primaryKeys = context.Model.GetEntityTypes().SelectMany(x =>
9692
{
9793
var primaryKey = x.FindPrimaryKey();
@@ -107,21 +103,16 @@ private void SetConstraintDetails(DbContext context, UniqueConstraintException e
107103
return Array.Empty<IndexDetails>();
108104
}
109105

110-
return new[]
111-
{ new IndexDetails(primaryKeyName, x.GetSchemaQualifiedTableName(), primaryKey.Properties) };
106+
return new [] { new IndexDetails(primaryKeyName, x.GetSchemaQualifiedTableName(), primaryKey.Properties) };
112107
});
113108

114109
uniqueIndexDetailsList = mappedIndexes
115110
.Union(primaryKeys)
116111
.ToList();
117112
}
118113

119-
var matchingIndexes = uniqueIndexDetailsList.Where(index =>
120-
providerException.Message.Contains(index.Name, StringComparison.OrdinalIgnoreCase)).ToList();
121-
var match = matchingIndexes.Count == 1
122-
? matchingIndexes[0]
123-
: matchingIndexes.FirstOrDefault(index =>
124-
providerException.Message.Contains(index.SchemaQualifiedTableName, StringComparison.OrdinalIgnoreCase));
114+
var matchingIndexes = uniqueIndexDetailsList.Where(index => providerException.Message.Contains(index.Name, StringComparison.OrdinalIgnoreCase)).ToList();
115+
var match = matchingIndexes.Count == 1 ? matchingIndexes[0] : matchingIndexes.FirstOrDefault(index => providerException.Message.Contains(index.SchemaQualifiedTableName, StringComparison.OrdinalIgnoreCase));
125116

126117
if (match != null)
127118
{
@@ -131,29 +122,19 @@ private void SetConstraintDetails(DbContext context, UniqueConstraintException e
131122
}
132123
}
133124

134-
private void SetConstraintDetails(DbContext context, ReferenceConstraintException exception,
135-
TProviderException providerException)
125+
private void SetConstraintDetails(DbContext context, ReferenceConstraintException exception, TProviderException providerException)
136126
{
137127
if (foreignKeyDetailsList == null)
138128
{
139129
var keys = context.Model.GetEntityTypes().SelectMany(x => x.GetDeclaredForeignKeys());
140130

141-
var mappedConstraints = keys.SelectMany(index => index.GetMappedConstraints(),
142-
(index, constraint) => new { constraint, index.Properties });
131+
var mappedConstraints = keys.SelectMany(index => index.GetMappedConstraints(), (index, constraint) => new { constraint, index.Properties });
143132

144-
foreignKeyDetailsList = mappedConstraints.Select(arg =>
145-
new ForeignKeyDetails(arg.constraint.Name, arg.constraint.Table.SchemaQualifiedName,
146-
arg.Properties))
147-
.ToList();
133+
foreignKeyDetailsList = mappedConstraints.Select(arg => new ForeignKeyDetails(arg.constraint.Name, arg.constraint.Table.SchemaQualifiedName, arg.Properties)).ToList();
148134
}
149135

150-
var matchingForeignKeys = foreignKeyDetailsList.Where(foreignKey =>
151-
providerException.Message.Contains(foreignKey.Name, StringComparison.OrdinalIgnoreCase)).ToList();
152-
var match = matchingForeignKeys.Count == 1
153-
? matchingForeignKeys[0]
154-
: matchingForeignKeys.FirstOrDefault(foreignKey =>
155-
providerException.Message.Contains(foreignKey.SchemaQualifiedTableName,
156-
StringComparison.OrdinalIgnoreCase));
136+
var matchingForeignKeys = foreignKeyDetailsList.Where(foreignKey => providerException.Message.Contains(foreignKey.Name, StringComparison.OrdinalIgnoreCase)).ToList();
137+
var match = matchingForeignKeys.Count == 1 ? matchingForeignKeys[0] : matchingForeignKeys.FirstOrDefault(foreignKey => providerException.Message.Contains(foreignKey.SchemaQualifiedTableName, StringComparison.OrdinalIgnoreCase));
157138

158139
if (match != null)
159140
{
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using EntityFramework.Exceptions.Common;
23
using Microsoft.EntityFrameworkCore;
34

@@ -6,22 +7,22 @@
67
namespace EntityFramework.Exceptions.MySQL.Pomelo;
78
#else
89
using MySql.Data.MySqlClient;
9-
1010
namespace EntityFramework.Exceptions.MySQL;
1111
#endif
1212

1313
class MySqlExceptionProcessorInterceptor : ExceptionProcessorInterceptor<MySqlException>
1414
{
1515
protected override DatabaseError? GetDatabaseError(MySqlException dbException)
1616
{
17+
1718
#if POMELO
1819
return dbException.ErrorCode switch
1920
#else
2021
return (MySqlErrorCode)dbException.Number switch
2122
#endif
2223
{
2324
MySqlErrorCode.ColumnCannotBeNull => DatabaseError.CannotInsertNull,
24-
MySqlErrorCode.DuplicateKeyEntry => DatabaseError.UniqueConstraint,
25+
MySqlErrorCode.DuplicateKeyEntry=> DatabaseError.UniqueConstraint,
2526
MySqlErrorCode.WarningDataOutOfRange => DatabaseError.NumericOverflow,
2627
MySqlErrorCode.DataTooLong => DatabaseError.MaxLength,
2728
MySqlErrorCode.NoReferencedRow => DatabaseError.ReferenceConstraint,
@@ -35,10 +36,13 @@ class MySqlExceptionProcessorInterceptor : ExceptionProcessorInterceptor<MySqlEx
3536

3637
public static class ExceptionProcessorExtensions
3738
{
38-
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self) =>
39-
self.AddInterceptors(new MySqlExceptionProcessorInterceptor());
39+
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self)
40+
{
41+
return self.AddInterceptors(new MySqlExceptionProcessorInterceptor());
42+
}
4043

41-
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(
42-
this DbContextOptionsBuilder<TContext> self) where TContext : DbContext =>
43-
self.AddInterceptors(new MySqlExceptionProcessorInterceptor());
44+
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(this DbContextOptionsBuilder<TContext> self) where TContext : DbContext
45+
{
46+
return self.AddInterceptors(new MySqlExceptionProcessorInterceptor());
47+
}
4448
}

EntityFramework.Exceptions.Oracle/OracleExceptionProcessorInterceptor.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OracleExceptionProcessorInterceptor : ExceptionProcessorInterceptor<Oracle
1212
private const int ChildRecordFound = 2292;
1313
private const int NumericOverflow = 1438;
1414
private const int NumericOrValueError = 12899;
15-
15+
1616
protected override DatabaseError? GetDatabaseError(OracleException dbException)
1717
{
1818
return dbException.Number switch
@@ -27,14 +27,17 @@ class OracleExceptionProcessorInterceptor : ExceptionProcessorInterceptor<Oracle
2727
};
2828
}
2929
}
30-
30+
3131
public static class ExceptionProcessorExtensions
3232
{
33-
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self) =>
34-
self.AddInterceptors(new OracleExceptionProcessorInterceptor());
33+
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self)
34+
{
35+
return self.AddInterceptors(new OracleExceptionProcessorInterceptor());
36+
}
3537

36-
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(
37-
this DbContextOptionsBuilder<TContext> self)
38-
where TContext : DbContext =>
39-
self.AddInterceptors(new OracleExceptionProcessorInterceptor());
38+
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(this DbContextOptionsBuilder<TContext> self)
39+
where TContext : DbContext
40+
{
41+
return self.AddInterceptors(new OracleExceptionProcessorInterceptor());
42+
}
4043
}

EntityFramework.Exceptions.PostgreSQL/PostgresExceptionProcessorInterceptor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ class PostgresExceptionProcessorInterceptor : ExceptionProcessorInterceptor<Post
2222

2323
public static class ExceptionProcessorExtensions
2424
{
25-
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self) =>
26-
self.AddInterceptors(new PostgresExceptionProcessorInterceptor());
25+
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self)
26+
{
27+
return self.AddInterceptors(new PostgresExceptionProcessorInterceptor());
28+
}
2729

28-
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(
29-
this DbContextOptionsBuilder<TContext> self) where TContext : DbContext =>
30-
self.AddInterceptors(new PostgresExceptionProcessorInterceptor());
30+
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(this DbContextOptionsBuilder<TContext> self) where TContext : DbContext
31+
{
32+
return self.AddInterceptors(new PostgresExceptionProcessorInterceptor());
33+
}
3134
}

EntityFramework.Exceptions.SqlServer/SqlServerExceptionProcessorInterceptor.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
namespace EntityFramework.Exceptions.SqlServer;
66

7-
class SqlServerExceptionProcessorInterceptor : ExceptionProcessorInterceptor<SqlException>
7+
class SqlServerExceptionProcessorInterceptor: ExceptionProcessorInterceptor<SqlException>
88
{
99
private const int ReferenceConstraint = 547;
1010
private const int CannotInsertNull = 515;
1111
private const int CannotInsertDuplicateKeyUniqueIndex = 2601;
1212
private const int CannotInsertDuplicateKeyUniqueConstraint = 2627;
1313
private const int ArithmeticOverflow = 8115;
1414
private const int StringOrBinaryDataWouldBeTruncated = 8152;
15-
15+
1616
//SQL Server 2019 added a new error with better error message: https://docs.microsoft.com/en-us/archive/blogs/sql_server_team/string-or-binary-data-would-be-truncated-replacing-the-infamous-error-8152
1717
private const int StringOrBinaryDataWouldBeTruncated2019 = 2628;
18-
18+
1919
protected override DatabaseError? GetDatabaseError(SqlException dbException)
2020
{
2121
return dbException.Number switch
@@ -34,10 +34,13 @@ class SqlServerExceptionProcessorInterceptor : ExceptionProcessorInterceptor<Sql
3434

3535
public static class ExceptionProcessorExtensions
3636
{
37-
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self) =>
38-
self.AddInterceptors(new SqlServerExceptionProcessorInterceptor());
37+
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self)
38+
{
39+
return self.AddInterceptors(new SqlServerExceptionProcessorInterceptor());
40+
}
3941

40-
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(
41-
this DbContextOptionsBuilder<TContext> self) where TContext : DbContext =>
42-
self.AddInterceptors(new SqlServerExceptionProcessorInterceptor());
42+
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(this DbContextOptionsBuilder<TContext> self) where TContext : DbContext
43+
{
44+
return self.AddInterceptors(new SqlServerExceptionProcessorInterceptor());
45+
}
4346
}

EntityFramework.Exceptions.Sqlite/SqliteExceptionProcessorInterceptor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ class SqliteExceptionProcessorInterceptor : ExceptionProcessorInterceptor<Sqlite
2828

2929
public static class ExceptionProcessorExtensions
3030
{
31-
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self) =>
32-
self.AddInterceptors(new SqliteExceptionProcessorInterceptor());
31+
public static DbContextOptionsBuilder UseExceptionProcessor(this DbContextOptionsBuilder self)
32+
{
33+
return self.AddInterceptors(new SqliteExceptionProcessorInterceptor());
34+
}
3335

34-
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(
35-
this DbContextOptionsBuilder<TContext> self) where TContext : DbContext =>
36-
self.AddInterceptors(new SqliteExceptionProcessorInterceptor());
36+
public static DbContextOptionsBuilder<TContext> UseExceptionProcessor<TContext>(this DbContextOptionsBuilder<TContext> self) where TContext : DbContext
37+
{
38+
return self.AddInterceptors(new SqliteExceptionProcessorInterceptor());
39+
}
3740
}

EntityFramework.Exceptions.Tests/DatabaseTests.cs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ protected DatabaseTests(DemoContext demoContext, SameNameIndexesContext sameName
2222
DemoContext = demoContext;
2323
SameNameIndexesContext = sameNameIndexesContext;
2424

25-
isMySql = MySqlDatabaseFacadeExtensions.IsMySql(DemoContext.Database) ||
26-
MySQLDatabaseFacadeExtensions.IsMySql(DemoContext.Database);
25+
isMySql = MySqlDatabaseFacadeExtensions.IsMySql(DemoContext.Database) || MySQLDatabaseFacadeExtensions.IsMySql(DemoContext.Database);
2726
isSqlite = demoContext.Database.IsSqlite();
2827
}
2928

@@ -53,10 +52,8 @@ public virtual async Task UniqueColumnViolationThrowsUniqueConstraintExceptionTh
5352
DemoContext.Products.Add(new Product { Name = "Bulk Update 2" });
5453

5554
await DemoContext.SaveChangesAsync();
56-
Assert.Throws<UniqueConstraintException>(() =>
57-
DemoContext.Products.ExecuteUpdate(p => p.SetProperty(pp => pp.Name, "Bulk Update 1")));
58-
await Assert.ThrowsAsync<UniqueConstraintException>(async () =>
59-
await DemoContext.Products.ExecuteUpdateAsync(p => p.SetProperty(pp => pp.Name, "Bulk Update 1")));
55+
Assert.Throws<UniqueConstraintException>(() => DemoContext.Products.ExecuteUpdate(p => p.SetProperty(pp => pp.Name, "Bulk Update 1")));
56+
await Assert.ThrowsAsync<UniqueConstraintException>(async () => await DemoContext.Products.ExecuteUpdateAsync(p => p.SetProperty(pp => pp.Name, "Bulk Update 1")));
6057
await DemoContext.Products
6158
.Where(p => p.Name == "Bulk Update 1" || p.Name == "Bulk Update 2")
6259
.ExecuteDeleteAsync();
@@ -76,14 +73,13 @@ public virtual async Task UniqueColumnViolationSameNamesIndexesInDifferentSchema
7673
{
7774
Name = "Rope Access"
7875
});
79-
76+
8077
SameNameIndexesContext.IncidentCategories.Add(new EFExceptionSchema.Entities.Incidents.Category
8178
{
8279
Name = "Rope Access"
8380
});
8481

85-
var uniqueConstraintException =
86-
Assert.Throws<UniqueConstraintException>(() => SameNameIndexesContext.SaveChanges());
82+
var uniqueConstraintException = Assert.Throws<UniqueConstraintException>(() => SameNameIndexesContext.SaveChanges());
8783
await Assert.ThrowsAsync<UniqueConstraintException>(() => SameNameIndexesContext.SaveChangesAsync());
8884

8985
if (!isSqlite)
@@ -115,7 +111,7 @@ public virtual async Task PrimaryKeyViolationThrowsUniqueConstraintException()
115111
Assert.False(string.IsNullOrEmpty(uniqueConstraintException.ConstraintName));
116112
Assert.False(string.IsNullOrEmpty(uniqueConstraintException.SchemaQualifiedTableName));
117113
Assert.NotEmpty(uniqueConstraintException.ConstraintProperties);
118-
Assert.Contains<string>(nameof(Product.Id), uniqueConstraintException.ConstraintProperties);
114+
Assert.Contains<string>(nameof(Product.Id), uniqueConstraintException.ConstraintProperties);
119115
}
120116
}
121117

@@ -135,10 +131,8 @@ public virtual async Task RequiredColumnViolationThrowsCannotInsertNullException
135131
DemoContext.Products.Add(new Product { Name = "Bulk Update 1" });
136132
await DemoContext.SaveChangesAsync();
137133

138-
Assert.Throws<CannotInsertNullException>(() =>
139-
DemoContext.Products.ExecuteUpdate(p => p.SetProperty(pp => pp.Name, (string)null)));
140-
await Assert.ThrowsAsync<CannotInsertNullException>(async () =>
141-
await DemoContext.Products.ExecuteUpdateAsync(p => p.SetProperty(pp => pp.Name, (string)null)));
134+
Assert.Throws<CannotInsertNullException>(() => DemoContext.Products.ExecuteUpdate(p => p.SetProperty(pp => pp.Name, (string)null)));
135+
await Assert.ThrowsAsync<CannotInsertNullException>(async () => await DemoContext.Products.ExecuteUpdateAsync(p => p.SetProperty(pp => pp.Name, (string)null)));
142136
await DemoContext.Products.Where(p => p.Name == "Bulk Update 1").ExecuteDeleteAsync();
143137
}
144138
#endif
@@ -174,16 +168,14 @@ void Query()
174168
{
175169
DemoContext.Products
176170
.Where(p => p.Name == "Bulk Update 1")
177-
.ExecuteUpdate(p =>
178-
p.SetProperty(pp => pp.Name, new string('G', DemoContext.ProductNameMaxLength + 5)));
171+
.ExecuteUpdate(p => p.SetProperty(pp => pp.Name, new string('G', DemoContext.ProductNameMaxLength + 5)));
179172
}
180173

181174
async Task QueryAsync()
182175
{
183176
await DemoContext.Products
184177
.Where(p => p.Name == "Bulk Update 1")
185-
.ExecuteUpdateAsync(p =>
186-
p.SetProperty(pp => pp.Name, new string('G', DemoContext.ProductNameMaxLength + 5)));
178+
.ExecuteUpdateAsync(p => p.SetProperty(pp => pp.Name, new string('G', DemoContext.ProductNameMaxLength + 5)));
187179
}
188180
}
189181
#endif
@@ -312,8 +304,7 @@ public virtual async Task DatabaseUnrelatedExceptionThrowsOriginalException()
312304
public virtual async Task DeleteParentItemThrowsReferenceConstraintException()
313305
{
314306
var product = new Product { Name = "AN" };
315-
var productPriceHistory = new ProductPriceHistory
316-
{ Product = product, Price = 15.27m, EffectiveDate = DateTimeOffset.UtcNow };
307+
var productPriceHistory = new ProductPriceHistory { Product = product, Price = 15.27m, EffectiveDate = DateTimeOffset.UtcNow };
317308
DemoContext.ProductPriceHistories.Add(productPriceHistory);
318309
await DemoContext.SaveChangesAsync();
319310

@@ -331,8 +322,7 @@ public virtual async Task DeleteParentItemThrowsReferenceConstraintException()
331322
public virtual async Task DeleteParentItemThrowsReferenceConstraintExceptionThroughExecuteDelete()
332323
{
333324
var product = new Product { Name = "AN2" };
334-
var productPriceHistory = new ProductPriceHistory
335-
{ Product = product, Price = 15.27m, EffectiveDate = DateTimeOffset.UtcNow };
325+
var productPriceHistory = new ProductPriceHistory { Product = product, Price = 15.27m, EffectiveDate = DateTimeOffset.UtcNow };
336326
DemoContext.ProductPriceHistories.Add(productPriceHistory);
337327
await DemoContext.SaveChangesAsync();
338328

0 commit comments

Comments
 (0)