There are two ORM in my .NET Core 6.0 project, Entity Framework and Dapper. I would like to share the Fluent API with Dapper. Is there any way to do this? The main goal is to centralize the mapping of the entities.
It would be interesting if the dapper took advantage of the hasColumnName("String_Column_Name") of the fluent Api, to map the object according to Entity Framework.
For example: I have a view VW_PERSONAL_DATA that returns something like this:
ID, NAME, NAME_AND_SURNAME
and an entity:
public class PersonalData
{
public string? id { get; set; }
public string? MainName { get; set; }
public string? FullName { get; set; }
}
I have a Fluent API configuration file that looks like this:
public class PersonalDataConfig : IEntityTypeConfiguration<PersonalData>
{
public void Configure(EntityTypeBuilder<PersonalData> builder)
{
builder.ToView("VW_PERSONAL_DATA")
.HasNoKey();
builder.Property<string>(o => o.Id)
.HasColumnName("ID");
builder.Property<string>(o => o.MainName )
.HasColumnName("NAME");
builder.Property<string>(o => o.FullName)
.HasColumnName("NAME_AND_LASTNAME");
}
}
In my DbContext, I have this code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new PersonalDataConfig ());
}
When I use Entity Framework, it works.
private readonly IReadRepository<PersonalData> _repository;
var pd = (await _repository.ListAsync()).FirstOrDefault();
But when I use Dapper, it fails to map, and it's logical, because Dapper doesn't have its mapping configured:
private readonly IDapperRepository _repository;
var pd = await _repository.QueryFirstOrDefaultAsync<PersonalData>($"SELECT * FROM VW_PERSONAL_DATA", cancellationToken: cancellationToken);
I would like to take advantage of the mapper that Entity Framework uses also for Dapper, without having to rewrite everything again.
Centralizing all mappings in one place. Remembering that I use the Fluent API for migration. Therefore, it would be interesting to preserve it.
Please, I don't want us alias in query.
I hope that was clear. Thanks.