.net 您的数据库提供程序本机不支持“SqlQuery”方法中的错误

kb5ga3dv  于 2023-06-07  发布在  .NET
关注(0)|答案(1)|浏览(564)

在dbContext.Database.SqlQuery的函数执行调用中,出现以下异常:
'在'SqlQuery'方法中使用的元素类型'MyEntity'不受数据库提供程序的本机支持。使用受支持的元素类型,或使用ModelConfigurationBuilder.DefaultTypeMapping为类型定义Map。'
Exception details
源代码。

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));
        }
    }
}

测试代码。

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);
        }
    }
}

添加了建议的设置,以返回数据库提供程序本机不支持的类型。
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>();
        }
2jcobegt

2jcobegt1#

不完全是一个解决方案,但在EF8到来之前是一个变通方案:

statement += " for json path";
var asJson = string.Join("", _context.Database.SqlQueryRaw<string>(statement)); 
return System.Text.Json.JsonSerializer.Deserialize<TModel[]>(asJson);

相关问题