efcore icon indicating copy to clipboard operation
efcore copied to clipboard

ComplexProperty column name mapping ignored when loading via FromSql()

Open martin-indexeb opened this issue 2 years ago • 2 comments

My apologies if this is logged elsewhere, but I couldn't find any references to the same issue.

I've just upgraded a project to .Net Core 8, with EF Core 8.0.8, and ran into an issue with converting an Owned type to a ComplexProperty when trying to load data via a FromSql() method. I also found a workaround if anyone else comes across it.

As a quick sample, given the following:

public record DayInYear
{
  public int Day { get; set; }
  public int Month { get; set; }
}

public class Employee
{
  public int Id { get; set; }
  public required string Name { get; set; }
  public required DayInYear ReviewDay { get; set; }
}

public class EmployeeContext : DbContext
{
  public DbSet<Employee> Employees { get; set; }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Employee>(x =>
    {
      x.ComplexProperty(
        y => y.ReviewDay,
        y =>
        {
          y.IsRequired();
          y.Property(z => z.Day).HasColumnName("ReviewDayDay");
          y.Property(z => z.Month).HasColumnName("ReviewDayMonth");
        });
    });
    base.OnModelCreating(modelBuilder);
  }

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
    optionsBuilder.UseSqlite($@"Data Source=.\Employees.db");
    
    // You get the same issue with SQL Server
    //optionsBuilder.UseSqlServer("Server=localhost;Integrated Security=true;Initial Catalog=EmployeesDb;TrustServerCertificate=True");

    base.OnConfiguring(optionsBuilder);
  }
}

If you then try to access the data with using FromSql() the column name mapping is ignored and an error is thrown:


var db = new EmployeeContext();
db.Database.EnsureCreated();
try
{
  var list2 = db.Employees.FromSql($"select * from Employees").ToList();
  Console.WriteLine("Loaded employees by sql");
} catch (Exception ex)
{
  Console.WriteLine($"Failed to load employees by sql: {ex.Message}");
}

Will give the response: Failed to load employees by sql: The required column 'ReviewDay_Day' was not present in the results of a 'FromSql' operation.

However, for the workaround, if you alias the column names to what it is expecting, it loads okay:


var db = new EmployeeContext();
db.Database.EnsureCreated();
try
{
  var list2 = db.Employees.FromSql($"select Id, Name, ReviewDayDay as ReviewDay_Day, ReviewDayMonth as ReviewDay_Month from Employees").ToList();
  Console.WriteLine("Loaded employees by workaround sql");
} catch (Exception ex)
{
  Console.WriteLine($"Failed to load employees by amended sql: {ex.Message}");
}

I've created a sample project if it helps: https://github.com/martin-indexeb/ComplexPropertyFromSql

martin-indexeb avatar Oct 03 '24 09:10 martin-indexeb

Still repros on main

maumar avatar Oct 04 '24 07:10 maumar

Duplicate of https://github.com/dotnet/efcore/issues/34627

AndriySvyryd avatar Oct 04 '24 22:10 AndriySvyryd