You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> The code shown here comes from the [NewInEFCore9.CompiledModels](https://github.com/dotnet/EntityFramework.Docs/tree/main/samples/core/Miscellaneous/NewInEFCore9.CompiledModels/) sample.
571
+
572
+
Compiled models can improve startup time for applications with large models--that is entity type counts in the 100s or 1000s. In previous versions of EF Core, a compiled model had to be generated manually, using the command line. For example:
573
+
574
+
```dotnetcli
575
+
dotnet ef dbcontext optimize
576
+
```
577
+
578
+
After running the command, a line like, `.UseModel(MyCompiledModels.BlogsContextModel.Instance)` must be added to `OnConfiguring` to tell EF Core to use the compiled model.
579
+
580
+
Starting with EF9, this `.UseModel` line is no longer needed. Instead, the compiled model will be detected and used automatically. This can be seen by having EF log whenever it is building the model. Running a simple application then shows EF building the model when the application starts:
581
+
582
+
```output
583
+
Starting application...
584
+
>> EF is building the model...
585
+
Model loaded model with 2 entity types.
586
+
```
587
+
588
+
The output from running `dotnet ef dbcontext optimize` on the model project is:
589
+
590
+
```output
591
+
PS D:\code\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore9.CompiledModels\Model> dotnet ef dbcontext optimize
592
+
593
+
Build succeeded in 0.3s
594
+
595
+
Build succeeded in 0.3s
596
+
Build started...
597
+
Build succeeded.
598
+
>> EF is building the model...
599
+
>> EF is building the model...
600
+
Successfully generated a compiled model, it will be discovered automatically, but you can also call 'options.UseModel(BlogsContextModel.Instance)'. Run this command again when the model is modified.
Notice that the log output indicates that the _model was built when running the command_. If we now run the application again, after rebuilding but without making any code changes, then the output is:
605
+
606
+
```output
607
+
Starting application...
608
+
Model loaded model with 2 entity types.
609
+
```
610
+
611
+
Notice that the model was not built when starting the application because the compiled model was detected and used automatically.
612
+
613
+
### MSBuild integration
614
+
615
+
With the above approach, the compiled model still needs to be regenerated manually when the entity types or `DbContext` configuration is changed. However, EF9 ships with MSBuild and targets package that can automatically update the compiled model when the model project is built! To get started, install the [Microsoft.EntityFrameworkCore.Tasks](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tasks/) NuGet package. For example:
> Use the package version in the command above that matches the version of EF Core that you are using.
623
+
624
+
Then enable the integration by setting the `EFOptimizeContext` property to your `.csproj` file. For example:
625
+
626
+
```xml
627
+
<PropertyGroup>
628
+
<EFOptimizeContext>true</EFOptimizeContext>
629
+
</PropertyGroup>
630
+
```
631
+
632
+
There are additional, optional, MSBuild properties for controlling how the model is built, equivalent to the options passed on the command line to `dotnet ef dbcontext optimize`. These include:
| EFOptimizeContext | Set to `true` to enable auto-compiled models. |
637
+
| DbContextName | The DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will find the context class. If there are multiple context classes, this option is required. |
638
+
| EFStartupProject | Relative path to the project folder of the startup project. Default value is the current folder. |
639
+
| EFTargetNamespace | The namespace to use for all generated classes. Defaults to generated from the root namespace and the output directory plus CompiledModels. |
640
+
641
+
In our example, we need to specify the startup project:
And running the application shows that the compiled model has been detected and hence the model is not built again:
672
+
673
+
```output
674
+
Starting application...
675
+
Model loaded model with 2 entity types.
676
+
```
677
+
678
+
Now, whenever the model changes, the compiled model will be automatically rebuilt as soon as the project is built.
679
+
680
+
> [NOTE!]
681
+
> We are working through some performance issues with changes made to the compiled model in EF8 and EF9. See [Issue 33483#](https://github.com/dotnet/efcore/issues/33483) for more information.
0 commit comments