I have an entity model generated by dotnet ef dbcontext scaffold
.
It generated (among others) a class for this table:
CREATE TABLE MyTable
(
Id INT IDENTITY(1,1) NOT NULL,
ProductId INT NOT NULL,
CountryId NVARCHAR(3) NOT NULL
);
Class:
public class MyTable
{
public int Id { get; set; }
public int ProductId { get; set; }
public string CountryId { get; set; }
}
DbContext:
// extraction ...
public virtual DbSet<MyTable> MyTables { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyTable>(entity =>
{
entity.ToTable("MyTable");
entity.Property(e => e.CountryId).HasMaxLength(3);
}
}
// ...
After some time I made a change to this table:
CREATE TABLE MyTable
(
Id INT IDENTITY(1,1) NOT NULL,
ProductId INT NOT NULL,
TheCountryId INT NOT NULL -- Rename and change the Data type
);
And of course to the class and DbContext
as well.
Class:
public class MyTable
{
public int Id { get; set; }
public int ProductId { get; set; }
public int TheCountryId { get; set; } // Rename and change the Data type
}
DbContext:
// extraction ...
public virtual DbSet<MyTable> MyTables { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyTable>(entity =>
{
entity.ToTable("MyTable");
// Remove restriction
}
}
// ...
When I run _db.MyTables.ToArray()
or similar code, Entity Framework generates queries which still select CountryId
(and not TheCountryId
)
SELECT
[m.MyTable].[Id], [m.MyTable].[ProductId], [m.MyTable].[CountryId], [m.MyTable].[TheCountryId]
FROM
[MyTable] AS [m.MyTable]
Why does this happen? I removed all that indicates that the class had a property CountryId
. I also deleted the bin an obj folders to make sure the program is a clear compilation. Nothing works. Is Entity Framework Core caching queries or is SQL Server doing this?