Cause.Core.DataLayerExtensions are a set of extensions for EntityFrameworkCore :
- Mapping classes.
- Mapping auto detection.
- Mapping naming convention.
If you want to create a mapping class for the foo model
public class SomeExample
{
public Guid Id {get; set;}
public ICollection<SomeOtherClass> Classes {get;set;}
}
You would simply create a mapping class that inherits from EntityMappingConfiguration<T>
and override the Map function. From there, it's all normal fluent mapping code.
public class SomeExampleMapping: EntityMappingConfiguration<SomeExample>
{
public override Map(EntityTypeBuilder<SomeXample> entity)
{
entity.HasKey(m => m.Id);
entity.HasMany(m => m.Classes)
.WithOne(m => m.Parent)
.HasForeignKey(m => m.ParentId);
}
}
Then, in you DbContext class, you can add the mapping from the OnModelCreating function like so:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new CallSubTypeMapping().Map(modelBuilder);
}
The project also contains a mapping auto detection feature so you can easily add all of your mapping in one line. It can be used like this from the OnModelCreating function from you DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
this.UseAutoDetectedMappings(modelBuilder);
}
Note that your mapping classes must be in your DbContext's project to be detected.
All of these features must be used from the DbContext's OnModelCreating function.
public class SomeClass
{
[PrimaryKey]
public Guid Id {get;set;}
public string CurrentName {get;set;}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.AddTableNameToPrimaryKey();
}
Will add the class name to the primary key.
Id
would be mapped asIdSomeClass
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseTablePrefix("Tbl")
}
Add "Tbl" in front of the class name.
SomeClass
would then be mapped asTblSomeClass
.
You can also easily auto map everything to snake case.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseAutoSnakeCaseMapping();
}
Automatically transform all class and property naming mapping to snake case.
SomeClass
would be mapped assome_class
.CurrentName
would becurrent_name
.
You can use any or all of the naming feature together like so:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.AddTableNameToPrimaryKey();
modelBuilder.UseAutoSnakeCaseMapping();
modelBuilder.UseTablePrefix("tbl_");
this.UseAutoDetectedMappings(modelBuilder);
}
That would create a table named like this:
- tbl_some_class
With these fields :
- id_some_class
- current_name