"Invalid column name" MVC

-1

I have a table in a DbContext , I change its name, I update the database and when I run I am redirected to Visual Studio with the following error message:

  

Invalid column name 'SetorId'.

Note: The table name is Set_Id. I change the name in model and in the table itself before updating the database .

Model:

public class RamalModel
{
    public int ID { get; set; }

    [Required(ErrorMessage = "O nome é obrigatório.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "O número é obrigatório.")]
    public int Numero { get; set; }

    [ForeignKey("Setor")]
    public int SetorId { get; set; }

    public Setor Setor { get; set; }
}

DbContext:

public class RamaDb : DbContext
{
    public DbSet<Usuario> Usuarios { get; set; }

    public DbSet<RamalModel> Ramais { get; set; }

    public DbSet<Setor> Setores { get; set; }

    public DbSet<Area> Areas { get; set; }
}

Table:

   CREATE TABLE [dbo].[RamalModels] (
[ID]       INT            IDENTITY (1, 1) NOT NULL,
[Nome]     NVARCHAR (MAX) NOT NULL,
[Numero]   INT            NOT NULL,
[Setor_Id] INT            DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_dbo.RamalModels] PRIMARY KEY CLUSTERED ([ID] ASC)
);


GO
CREATE NONCLUSTERED INDEX [IX_SetorId]
ON [dbo].[RamalModels]([Setor_Id] ASC);
    
asked by anonymous 06.06.2016 / 21:49

1 answer

0

The bank column name is wrong. It should be SetorId , not Setor_Id . Do not use underscore to map your entity keys, as they are not part of the Entity Framework convention.

You are using the wrong Entity Framework. It is he who should update the bank automatically for you, not you update the database with scripts.

    
06.06.2016 / 22:00