Invalid column name when trying to select on database via EF Core

1

I am trying to do a simple select on the database, everything is designed database-first and scaffolded correctly, the relationships are correct. In fact columns are named invalid that aren't a foreign key. It doesn't matter which entity I am trying to select, everything fails with an arbitrary amount of columns named that are invalid.

An example: the scaffolded class looks like this:

public partial class Product
{  
        public Guid ProductId { get; set; }
        public string Name { get; set; }

        public string License { get; set; }
        public bool RatingOnlyInd { get; set; }
        public bool? ActiveInd { get; set; }
}

And an excerpt of the Modelbuilder:

 modelBuilder.Entity<Product>(entity =>
            {
                entity.Property(e => e.ProductId).ValueGeneratedNever();

                entity.Property(e => e.ActiveInd)
                    .IsRequired()
                    .HasDefaultValueSql("((1))");

                entity.Property(e => e.License)
                    .HasMaxLength(40)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(4000);
            });

I have only included the properties that are named in the error (they are the ones being selected). It seems very arbitrary why only these columns are invalid and not for example Name as well. I can provide other examples if you need them.

The internal server error I get:

System.Data.SqlClient.SqlException (0x80131904): Invalid column name "ActiveInd".
Invalid column name "License".
Invalid column name "RatingOnlyInd".

When I use SQL Server Profiler, I can see that the SQL being generated is fine:

SELECT [p].[Name], [p].[ProductId], [p].[License], [p].[RatingOnlyInd]
FROM [Product] AS [p]
WHERE [p].[ActiveInd] = 1 

The C# code:

new MyContext().Product
               .Where(p => p.ActiveInd == true)
               .Select(p => new { p.Name, p.ProductId, p.License, p.RatingOnlyInd })
               .ToList<object>();

And when I execute the exact same SQL directly on the database everything works. So it has to be Entity Framework Core, I assume?

I am using ASP.NET Core 3.1, Entity Framework Core 3.1.7 and C# 8.

If you need more information please let me know.

c#
sql-server
entity-framework-core
asp.net-core-3.1
asked on Stack Overflow Aug 14, 2020 by Carlos • edited Aug 14, 2020 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0