Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ private IEntityFinder Finder([DynamicallyAccessedMembers(IEntityType.Dynamically
var entityType = Model.FindEntityType(type);
if (entityType == null)
{
if (Model.IsShared(type))
if (Model.IsShared(type)
|| Model.FindEntityTypes(type).Any(e => e.HasSharedClrType))
{
throw new InvalidOperationException(CoreStrings.InvalidSetSharedType(type.ShortDisplayName()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/EFCore/EFCore.baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -4114,6 +4114,9 @@
{
"Member": "static string InvalidSetTypeOwned(object? typeName, object? ownerType);"
},
{
"Member": "static string InvalidSetTypeWithName(object? typeName, object? entityTypeName);"
},
{
"Member": "static string InvalidStructuredJsonPathIndexCount(object? indicesCount, object? arraySegmentCount);"
},
Expand Down
9 changes: 8 additions & 1 deletion src/EFCore/Internal/InternalDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ public override IEntityType EntityType

if (field == null)
{
if (_context.Model.IsShared(typeof(TEntity)))
if (_entityTypeName != null)
{
throw new InvalidOperationException(
CoreStrings.InvalidSetTypeWithName(typeof(TEntity).ShortDisplayName(), _entityTypeName));
}

if (_context.Model.IsShared(typeof(TEntity))
|| _context.Model.FindEntityTypes(typeof(TEntity)).Any(e => e.HasSharedClrType))
{
throw new InvalidOperationException(CoreStrings.InvalidSetSharedType(typeof(TEntity).ShortDisplayName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp

if (entityType == null)
{
if (_model.IsShared(entityClrType))
if (_model.IsShared(entityClrType)
|| ((IModel)_model).FindEntityTypes(entityClrType).Any(e => e.HasSharedClrType))
{
throw new InvalidOperationException(CoreStrings.InvalidSetSharedType(entityClrType.ShortDisplayName()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Internal/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IEnumerable<IEntityType> GetEntityTypesInHierarchicalOrder(this IM
/// </summary>
public static string? FindSameTypeNameWithDifferentNamespace(this IModel model, Type type)
=> model.GetEntityTypes()
.Where(x => x.ClrType.DisplayName(false) == type.DisplayName(false))
.Where(x => x.ClrType != type && x.ClrType.DisplayName(false) == type.DisplayName(false))
.Select(x => x.ClrType.DisplayName())
.FirstOrDefault();
}
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@
<data name="InvalidSetTypeOwned" xml:space="preserve">
<value>Cannot create a DbSet for '{typeName}' because it is configured as an owned entity type and must be accessed through its owning entity type '{ownerType}'. See https://aka.ms/efcore-docs-owned for more information.</value>
</data>
<data name="InvalidSetTypeWithName" xml:space="preserve">
<value>Cannot create a DbSet for '{typeName}' with entity type name '{entityTypeName}' because no entity type with this name is included in the model for the context.</value>
</data>
<data name="InvalidStructuredJsonPathIndexCount" xml:space="preserve">
<value>The number of indices provided ({indicesCount}) must match the number of array segments in the JSON path ({arraySegmentCount}).</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions test/EFCore.Tests/DbContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public void Set_throws_for_type_not_in_model_same_type_with_different_namespace(
typeof(DifferentNamespace.Category).DisplayName(), typeof(Category).DisplayName()), ex.Message);
}

[Fact]
public void Set_throws_for_entity_type_name_not_in_model()
{
using var context = new EarlyLearningCenter();
var ex = Assert.Throws<InvalidOperationException>(() => context.Set<Category>("Foo").Local);

Assert.Equal(
CoreStrings.InvalidSetTypeWithName(nameof(Category), "Foo"), ex.Message);
}

[Fact]
public void Local_calls_DetectChanges()
{
Expand Down
30 changes: 30 additions & 0 deletions test/EFCore.Tests/SharedTypeDbSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,36 @@ private class TheGu
public string ShirtColor { get; set; } = null!;
}

[Fact]
public void Set_throws_for_shared_entity_type_name_not_in_model()
{
using var context = new EarlyLearningCenter();
var ex = Assert.Throws<InvalidOperationException>(() => context.Set<Category>("Foo").Local);

Assert.Equal(
CoreStrings.InvalidSetTypeWithName(nameof(Category), "Foo"), ex.Message);

var exDict = Assert.Throws<InvalidOperationException>(() => context.Set<Dictionary<string, object>>("Foo").Local);

Assert.Equal(
CoreStrings.InvalidSetTypeWithName("Dictionary<string, object>", "Foo"), exDict.Message);
}

[Fact]
public void Set_throws_for_shared_entity_type_without_name()
{
using var context = new EarlyLearningCenter();
var ex = Assert.Throws<InvalidOperationException>(() => context.Set<Category>().Local);

Assert.Equal(
CoreStrings.InvalidSetSharedType(nameof(Category)), ex.Message);

var exDict = Assert.Throws<InvalidOperationException>(() => context.Set<Dictionary<string, object>>().Local);

Assert.Equal(
CoreStrings.InvalidSetSharedType("Dictionary<string, object>"), exDict.Message);
}

private class EarlyLearningCenter : DbContext
{
public DbSet<Product> Product1s
Expand Down
Loading