dotnet/efcore · error · InvalidOperationException
The type ' ' is configured as a shared-type entity type…
Error message
The type '{clrType}' is configured as a shared-type entity type, but the entity type name is not known. Ensure that CreateProxy is called on a DbSet created specifically for the shared-type entity type through use of a 'DbContext.Set' overload that accepts an entity type name. What it means
Thrown by ProxyFactory.Create when CreateProxy is invoked for a CLR type that is registered as a shared-type entity type (the same CLR type is used by multiple entity types, keyed by name) but no specific entity type name was supplied. FindRuntimeEntityType(type) returns null because the type alone is ambiguous, and IsShared(type) is true. Shared-type entities must be addressed through a named DbSet obtained via the Set(name) overload.
Solutions
- Obtain the DbSet via the named overload: var set = context.Set<T>("EntityName"); then call CreateProxy on that DbSet.
- Call CreateProxy through the DbSet rather than the generic serviceProvider/Type overload so the entity type name is known.
- If the type should map to a single entity type, stop configuring it as shared (remove duplicate Entity<T>(name) registrations).
Example fix
// before: shared-type entity, ambiguous by CLR type alone
var proxy = context.CreateProxy<Tag>(); // Tag is shared across named entity types
// after: resolve the named DbSet first
var set = context.Set<Tag>("OrderTag");
var proxy = set.CreateProxy(); Defensive patterns
Strategy: validation
Validate before calling
// Resolve a named DbSet for shared-type entities before creating a proxy
var entityType = context.Model.FindEntityType(typeof(Tag));
if (entityType == null && context.Model.IsShared(typeof(Tag)))
{
// Use the named overload instead of the generic CLR-type path
var set = context.Set<Tag>("OrderTag");
var proxy = set.CreateProxy();
}
else
{
var proxy = context.CreateProxy<Tag>();
} Type guard
static bool IsSharedType(IModel model, Type t) => model.IsShared(t);
Prevention
- Use context.Set<T>(name) for any CLR type registered under multiple named entity types.
- Avoid reusing one CLR type across several entity types unless necessary.
- Document shared-type entity names alongside the CLR type in your model.
When it happens
Trigger: Calling the generic CreateProxy<T> / CreateProxy(serviceProvider, typeof(T), ...) where T is a shared-type entity (e.g. used as a owned/weak type or multiple named entity types sharing one CLR type) without resolving a named DbSet first.
Common situations: Using the same CLR class (e.g. a Tag or Address DTO) as several distinct entity types via modelBuilder.Entity<T>(name); trying to materialize a proxy for that CLR type generically; DDD value-object-style reuse of one CLR type across entity types.
Related errors
- The entity type ' ' was not found. Ensure that the entity…
- Unable to create proxy for
- 'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each…
- Cosmos-specific methods can only be used when the context…
- ' ' cannot be configured for the parameter ' ' of the…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/807ce9bcd83060b7.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Proxies/Proxies/Internal/ProxyFactory.cs:42
private readonly ProxyGenerator _generator = new();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual object Create(
DbContext context,
Type type,
params object[] constructorArguments)
{
var entityType = context.Model.FindRuntimeEntityType(type);
if (entityType == null)
{
if (context.Model.IsShared(type))
{
throw new InvalidOperationException(ProxiesStrings.EntityTypeNotFoundShared(type.ShortDisplayName()));
}
throw new InvalidOperationException(CoreStrings.EntityTypeNotFound(type.ShortDisplayName()));
}
return CreateProxy(context, entityType, constructorArguments);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Type CreateProxyType(
IEntityType entityType)
=> _generator.ProxyBuilder.CreateClassProxyType(
entityType.ClrType,View on GitHub (pinned to 3a2006ef56)