0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NCC-2909-M
  • 709
  • 1
  • 7
  • 15
  • Are you configuring a relation between this table and the Countries table somewhere else? – CodeCaster Feb 15 '18 at 08:42
  • After changing the structure of the database, you don't have to manually change the entities. You can run the command (`dotnet ef dbcontext scaffold`) for scaffolding again. Can you do that, let us know if you still seeing the issue please. – Jaliya Udagedara Feb 15 '18 at 09:15

0 Answers0