In function execution calls of dbContext.Database.SqlQuery<T> the following exception:
'The element type 'MyEntity' used in 'SqlQuery' method is not natively supported by your database provider. Either use a supported element type, or use ModelConfigurationBuilder.DefaultTypeMapping to define a mapping for your type.'
Exception details
The source code.
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Test
{
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class EmployeeInfo
{
public int EmployeeId { get; set; }
public string Name { get; set; }
}
public class EmployeeDBContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public EmployeeDBContext() { }
public EmployeeDBContext(DbContextOptions<EmployeeDBContext> options)
: base(options) { }
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
base.ConfigureConventions(configurationBuilder);
//Action<TypeMappingConfigurationBuilder<EmployeeInfo>> actionEmployeeInfo = (buildAction) => { };
//configurationBuilder.DefaultTypeMapping(actionEmployeeInfo);
//Action<TypeMappingConfigurationBuilder> action = (buildAction) => { };
//configurationBuilder.DefaultTypeMapping(typeof(EmployeeInfo), action);
//configurationBuilder.DefaultTypeMapping(typeof(EmployeeInfo));
configurationBuilder.DefaultTypeMapping<EmployeeInfo>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data source=localhost;Database=EmployeeDB;User Id=sa;Password=Dev.NET;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>(builder => builder.HasKey(x => x.Id));
}
}
}
The Test code.
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
namespace Test
{
[TestFixture]
public class EmployeeTest
{
[Test(Description = nameof(Find_Employee_ReturnModel))]
public void Find_Employee_ReturnModel()
{
// Arrange
var dbContext = new EmployeeDBContext();
dbContext.Database.EnsureDeleted();
dbContext!.Database.EnsureCreated();
var employee = new Employee { FirstName = "Joao", LastName = "Gomes" };
dbContext.Employees.Add(employee);
dbContext.SaveChanges();
// Act
var employeeInfo = dbContext.Database.SqlQuery<EmployeeInfo>($"SELECT e.Id as EmployeeId, e.FirstName as Name FROM Employee e").ToList();
// Assert
employeeInfo.Should().HaveCount(1);
}
}
}
Added recommended settings to return a type that is not natively supported by the database provider.
DefaultTypeMapping
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
base.ConfigureConventions(configurationBuilder);
//Action<TypeMappingConfigurationBuilder<EmployeeInfo>> actionEmployeeInfo = (buildAction) => { };
//configurationBuilder.DefaultTypeMapping(actionEmployeeInfo);
//Action<TypeMappingConfigurationBuilder> action = (buildAction) => { };
//configurationBuilder.DefaultTypeMapping(typeof(EmployeeInfo), action);
//configurationBuilder.DefaultTypeMapping(typeof(EmployeeInfo));
configurationBuilder.DefaultTypeMapping<EmployeeInfo>();
}
Not exactly a solution but a workaround until EF8 comes:
statement += " for json path";
var asJson = string.Join("", _context.Database.SqlQueryRaw<string>(statement));
return System.Text.Json.JsonSerializer.Deserialize<TModel[]>(asJson);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With