Skip to content

Commit ab5b232

Browse files
CopilotAndriySvyryd
andcommitted
Add note about handling ambiguous type references in compiled models
Co-authored-by: AndriySvyryd <[email protected]>
1 parent b545bc7 commit ab5b232

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

entity-framework/core/cli/services.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ Some services used by the tools are only used at design time. These services are
1111

1212
[!code-csharp[Main](../../../samples/core/Miscellaneous/CommandLine/DesignTimeServices.cs#DesignTimeServices)]
1313

14+
### Customizing individual services
15+
16+
You can also override individual methods in design-time services. For example, to customize how type names are generated in scaffolded code to always use fully-qualified names (which can help avoid compilation errors due to ambiguous type references), you can override the `ShouldUseFullName` method in `CSharpHelper`:
17+
18+
```csharp
19+
public class MyDesignTimeServices : IDesignTimeServices
20+
{
21+
public void ConfigureDesignTimeServices(IServiceCollection services)
22+
=> services.AddSingleton<ICSharpHelper, MyCSharpHelper>();
23+
}
24+
25+
public class MyCSharpHelper : CSharpHelper
26+
{
27+
public MyCSharpHelper(ITypeMappingSource typeMappingSource) : base(typeMappingSource)
28+
{
29+
}
30+
31+
public override bool ShouldUseFullName(Type type)
32+
=> true; // Always use fully-qualified type names
33+
}
34+
```
35+
1436
## Referencing Microsoft.EntityFrameworkCore.Design
1537

1638
Microsoft.EntityFrameworkCore.Design is a DevelopmentDependency package. This means that the dependency won't flow transitively into other projects, and that you cannot, by default, reference its types.

entity-framework/core/performance/advanced-performance-topics.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ Because of these limitations, you should only use compiled models if your EF Cor
302302

303303
If supporting any of these features is critical to your success, then please vote for the appropriate issues linked above.
304304

305+
### Handling compilation errors due to ambiguous type references
306+
307+
When compiling models with types that have the same name but exist in different namespaces, the generated code may produce compilation errors due to ambiguous type references. To resolve this, you can customize the code generation to use fully-qualified type names by overriding `CSharpHelper.ShouldUseFullName` to return `true`. See [Design-time services](xref:core/cli/services) for information on how to override design-time services like `ICSharpHelper`.
308+
305309
## Reducing runtime overhead
306310

307311
As with any layer, EF Core adds a bit of runtime overhead compared to coding directly against lower-level database APIs. This runtime overhead is unlikely to impact most real-world applications in a significant way; the other topics in this performance guide, such as query efficiency, index usage and minimizing roundtrips, are far more important. In addition, even for highly-optimized applications, network latency and database I/O will usually dominate any time spent inside EF Core itself. However, for high-performance, low-latency applications where every bit of perf is important, the following recommendations can be used to reduce EF Core overhead to a minimum:

0 commit comments

Comments
 (0)