|
1 | 1 | # SpatialFocus.EntityFrameworkCore.Extensions |
| 2 | + |
2 | 3 | A set of useful extensions for EntityFrameworkCore (Enum Lookup Tables, Naming of tables / properties / keys, Pluralize). |
3 | 4 |
|
4 | 5 | [](https://spatial-focus.visualstudio.com/EntityFrameworkCore.Extensions/_build/latest?definitionId=3) |
5 | 6 | [](https://www.nuget.org/packages/SpatialFocus.EntityFrameworkCore.Extensions/) |
6 | 7 |
|
7 | 8 | ## Installation |
8 | 9 |
|
9 | | -``` |
| 10 | +```console |
10 | 11 | Install-Package SpatialFocus.EntityFrameworkCore.Extensions -Version 1.0.0-rc2 |
11 | 12 | ``` |
12 | 13 |
|
13 | 14 | ## Usage |
14 | 15 |
|
15 | | -For an example usage, see the `DemoContext` in the [sample project](https://github.com/SpatialFocus/SpatialFocus.EntityFrameworkCore.Extensions/tree/master/samples/SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo) for now. |
| 16 | +This extension provides two extension methods to modify the `modelBuilder` in the `OnModelCreating` method in the `DbContext` class. |
| 17 | + |
| 18 | +```csharp |
| 19 | +using SpatialFocus.EntityFrameworkCore.Extensions; |
| 20 | + |
| 21 | +public partial class MyContext : DbContext |
| 22 | +{ |
| 23 | + // ... |
| 24 | +
|
| 25 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 26 | + { |
| 27 | + modelBuilder.ConfigureEnumLookup(EnumLookupOptions.Default.UseStringAsIdentifier()); |
| 28 | + |
| 29 | + modelBuilder.ConfigureNames(NamingOptions.Default.SkipTableNamingForGenericEntityTypes()); |
| 30 | + |
| 31 | + // ... |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Configuration Options |
| 37 | + |
| 38 | +The two extension methods in the `OnModelCreating` method in the `DbContext` class: |
| 39 | + |
| 40 | +- **ConfigureEnumLookup(...)** allows you to define in which form lookup tables for *Enums* will be constructed and named: |
| 41 | + |
| 42 | + - **Default** defines the naming scheme for the table to use *snake_case* and to use the number lookup format. |
| 43 | + - **UseNumberAsIdentifier()** or **UseStringAsIdentifier()** defines whether the lookup table will be based on the string enum values or the numeric enum value as primary key in the resulting table and as foreign key in the relation. |
| 44 | + - **Singularize()** or **Pluralize()** defines whether the table names will be the singular or plural versions of the enum type. |
| 45 | + - **SetNamingScheme(...)** allows you to override the naming using one of the predefined schemes (see below) or a custom function. |
| 46 | + |
| 47 | +- **ConfigureNames(...)** allows you to define in which form tables, properties and constraints will be named: |
| 48 | + |
| 49 | + - **Default** defines the naming scheme for the elemens to use *snake_case* for naming and the *DbSet name* to name the tables. |
| 50 | + - **SetTableNamingSource(...)** defines which naming source to use (see below). It means whether to use the ClrType name or the DbSet name to name the tables. |
| 51 | + - **Singularize()** or **Pluralize()** defines whether the table names will be the singular or plural versions. |
| 52 | + - **SetNamingScheme(...)** allows you to override the naming using one of the predefined schemes (see below) or a custom function. |
| 53 | + - **OverrideTableNaming(...)**, **OverridePropertyNaming(...)**, **OverrideConstraintNaming(...)** to deviate from the general naming scheme. |
| 54 | + - **SkipEntireEntities(...)** and **SkipTableNamingForEntities(...)** to skip the naming for either the whole entity or just the table name for certain entities by using a `Func<IMutableEntityType, bool>` skip function, e.g. `SkipEntireEntities(entity => entity.Name == "MyEntity")`. |
| 55 | + - **SkipTableNamingForGenericEntityTypes()** should be used to avoid naming the Enum lookup tables which can lead to unwanted results. |
| 56 | + |
| 57 | +### Naming Schemes |
| 58 | + |
| 59 | +- NamingScheme.SnakeCase |
| 60 | +- NamingScheme.ScreamingSnakeCase |
| 61 | +- NamingScheme.KebabCase |
| 62 | +- Any custom `Func<string, string>`, e.g. `SetNamingScheme(name => name.ToLower())` |
| 63 | + |
| 64 | +#### Naming Source |
| 65 | + |
| 66 | +- NamingSource.ClrType |
| 67 | +- NamingSource.DbSet |
| 68 | + |
| 69 | +## Examples |
| 70 | + |
| 71 | +For an exemplary usage, see the `DemoContext` in the [sample project](https://github.com/SpatialFocus/SpatialFocus.EntityFrameworkCore.Extensions/tree/master/samples/SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo). |
16 | 72 |
|
17 | 73 | ---- |
18 | 74 |
|
|
0 commit comments