"Invalid column name" MVC

Asked

Viewed 259 times

-1

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

Invalid column name 'Setorid'.

Obs:The table name is Setor_id. I change the name on 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);

1 answer

0


The name of the column in bank is wrong. It should be SetorId, and 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. This is the one that should update the database automatically for you, and not you update the database with scripts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.