Skip to content

Commit af6b5c6

Browse files
committed
GetEntityType throws EntityTypeNotFoundException instead of ArgumentException
1 parent fe283d4 commit af6b5c6

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Runtime.CompilerServices;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
3+
4+
namespace Thinktecture.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Entity with provided name and/or type is not part of current <see cref="DbContext"/>.
8+
/// </summary>
9+
public class EntityTypeNotFoundException : ArgumentException
10+
{
11+
/// <summary>
12+
/// Initializes new instance of <see cref="EntityTypeNotFoundException"/>.
13+
/// </summary>
14+
/// <param name="entityName">Entity name not found in the model of current <see cref="DbContext"/>.</param>
15+
/// <param name="paramName">The name of the parameter.</param>
16+
public EntityTypeNotFoundException(
17+
string entityName,
18+
[CallerArgumentExpression("entityName")] string? paramName = null)
19+
: base($"The provided name '{entityName}' is not part of the provided Entity Framework model.", paramName)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes new instance of <see cref="EntityTypeNotFoundException"/>.
25+
/// </summary>
26+
/// <param name="entityType">The type of the entity not found in the model of current <see cref="DbContext"/>.</param>
27+
/// <param name="paramName">The name of the parameter.</param>
28+
public EntityTypeNotFoundException(
29+
Type entityType,
30+
[CallerArgumentExpression("entityType")] string? paramName = null)
31+
: base($"The provided type '{entityType.ShortDisplayName()}' is not part of the provided Entity Framework model.", paramName)
32+
{
33+
}
34+
35+
/// <summary>
36+
/// Initializes new instance of <see cref="EntityTypeNotFoundException"/>.
37+
/// </summary>
38+
/// <param name="entityName">Entity name not found in the model of current <see cref="DbContext"/>.</param>
39+
/// <param name="entityType">The type of the entity not found in the model of current <see cref="DbContext"/>.</param>
40+
/// <param name="entityNameParamName">The name of the parameter.</param>
41+
public EntityTypeNotFoundException(
42+
string entityName,
43+
Type entityType,
44+
[CallerArgumentExpression("entityName")] string? entityNameParamName = null)
45+
: base($"The provided name '{entityName}' and the type '{entityType.ShortDisplayName()}' were not part of the provided Entity Framework model.", entityNameParamName)
46+
{
47+
}
48+
}

src/Thinktecture.EntityFrameworkCore.Relational/Extensions/RelationalModelExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using Microsoft.EntityFrameworkCore.Infrastructure;
21
using Microsoft.EntityFrameworkCore.Metadata;
2+
using Thinktecture.EntityFrameworkCore;
33

4-
// ReSharper disable once CheckNamespace
54
namespace Thinktecture;
65

76
/// <summary>
@@ -29,7 +28,7 @@ public static IEntityType GetEntityType(this IModel model, Type type)
2928
var entityType = model.FindEntityType(type);
3029

3130
if (entityType == null)
32-
throw new ArgumentException($"The provided type '{type.ShortDisplayName()}' is not part of the provided Entity Framework model.", nameof(type));
31+
throw new EntityTypeNotFoundException(type);
3332

3433
return entityType;
3534
}
@@ -61,8 +60,8 @@ public static IEntityType GetEntityType(this IModel model, string name, Type? fa
6160
return entityType;
6261

6362
if (fallbackEntityType is not null)
64-
throw new ArgumentException($"The provided name '{name}' and the type '{fallbackEntityType.ShortDisplayName()}' were not part of the provided Entity Framework model.", nameof(name));
63+
throw new EntityTypeNotFoundException(name, fallbackEntityType);
6564

66-
throw new ArgumentException($"The provided name '{name}' is not part of the provided Entity Framework model.", nameof(name));
65+
throw new EntityTypeNotFoundException(name);
6766
}
6867
}

0 commit comments

Comments
 (0)