EFCore Integration

Motivation

Learn how Intellenum integrates with EFCore for seamless enum handling in database interactions.

How to

In your DbContext:

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
          {
          configurationBuilder.ApplyVogenEfConvertersFromAssembly(typeof(YOURDBCONTEXT).Assembly);
          }
          

Explanation

  • The ApplyVogenEfConvertersFromAssembly extension method scans the provided assembly for Value Objects generated by Vogen.
  • For each Value Object found, it determines if an EFCore converter is available.
  • If a converter is found, it configures EFCore to use it for that specific Value Object type.
  • This simplifies the process of integrating Value Objects with EFCore by automatically configuring the necessary converters.

Example

protected override void OnModelCreating(ModelBuilder builder)
          {
          builder.Entity(b =>
          {
          b.Property(e => e.Name)
          .HasConversion(new Name.EfCoreValueConverter());
          });
          }
          

Explanation

  • The HasConversion method is used to specify the conversion mechanism for a property.
  • In this example, the Name.EfCoreValueConverter is a custom converter generated by Vogen specifically for the Name Value Object.
  • This ensures proper serialization and deserialization of the Name value when interacting with the database.

Reference

For a more detailed walkthrough of EFCore integration with Value Objects, refer to the EFCore Integration HowTo page.

Key Points

  • Value Objects (VOs) can be used with EFCore.
  • Structs can be used as Value Objects directly without a converter.
  • Classes require a converter, generated when you add the EFCoreValueConverter conversion in the attribute.
  • The ApplyVogenEfConvertersFromAssembly extension method helps automate the process of integrating VOs with EFCore.

Code Snippets

Community Contributions

This topic is constantly evolving based on feedback and contributions from the community. If you have any suggestions or improvements, please contribute to the project or submit a pull request.


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects demonstrating the usage of Intellenum.
          
          <a class='local-link directory-link' data-ref="samples/Intellenum.Examples/" href="#samples/Intellenum.Examples/">samples/Intellenum.Examples/</a> - Contains various example projects demonstrating different aspects of Intellenum, such as serialization, conversion, syntax examples, types, typical scenarios, and more.
          
          <a class='local-link directory-link' data-ref="samples/WebApplication/" href="#samples/WebApplication/">samples/WebApplication/</a> - Contains a sample web application that uses Intellenum.
          
          <a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code of the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Benchmarks/" href="#src/Benchmarks/">src/Benchmarks/</a> - Contains benchmark tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum.CodeFixers/" href="#src/Intellenum.CodeFixers/">src/Intellenum.CodeFixers/</a> - Contains code fixers for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum.SharedTypes/" href="#src/Intellenum.SharedTypes/">src/Intellenum.SharedTypes/</a> - Contains shared types used across the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum/" href="#src/Intellenum/">src/Intellenum/</a> - Contains the main source code for the Intellenum library. This directory is further divided into subdirectories for diagnostics, extensions, generators, member building, properties, rules, static constructor building, templates, and more.
          
          <a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains test projects for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/AnalyzerTests/" href="#tests/AnalyzerTests/">tests/AnalyzerTests/</a> - Contains unit tests for the Intellenum analyzer.
          
          <a class='local-link directory-link' data-ref="tests/ConsumerTests/" href="#tests/ConsumerTests/">tests/ConsumerTests/</a> - Contains tests for consuming the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Intellenum.Tests/" href="#tests/Intellenum.Tests/">tests/Intellenum.Tests/</a> - Contains additional tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Shared/" href="#tests/Shared/">tests/Shared/</a> - Contains shared test files.
          
          <a class='local-link directory-link' data-ref="tests/SnapshotTests/" href="#tests/SnapshotTests/">tests/SnapshotTests/</a> - Contains snapshot tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Testbench/" href="#tests/Testbench/">tests/Testbench/</a> - Contains a test bench for the Intellenum library.