0

I'm using Entity Framework to generate my database model for my application, and now I'm trying to submit some data into my database but I'm getting the following error:

The entity type SEC_USER is not part of the model for the current context.

I have read a lot that sometimes this error is shown because the connection string is wrong, but I know that my connection string is fine because the first view that is shown in my application brings a lot of data from the database and it works perfectly.

The code that is throwing me the error is the following, specifically the line db.SEC_USER.Add(user);:

public ActionResult LogIn(Login log)
{
    bool enter = _2Secure.Common.Data.Access.ldap.SignIn(log.Username, log.Password);
    ViewBag.entra = enter;

    if (enter)
    {
        SEC_USER user = new SEC_USER();
        user.name = "Yesid Bejarano";
        user.isAdmin = 1;

        db.SEC_USER.Add(user);
        db.SaveChanges();

        return View("~/Views/Home/_GridViewPartial.cshtml");
    }
    else
        return View("~/Views/Login/LogIn.cshtml");
}

And my DbContext have this structure:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

public partial class VASARODEVEnti : DbContext
{
    public VASARODEVEnti()
        : base("name=VASARODEVEnti")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SEC_USER>().ToTable("SEC_USER");
    }

    public virtual DbSet<SEC_ACTION> SEC_ACTION { get; set; }
    public virtual DbSet<SEC_ROL> SEC_ROL { get; set; }
    public virtual DbSet<SEC_USER> SEC_USER { get; set; }
    public virtual DbSet<VAS_CATEGORY> VAS_CATEGORY { get; set; }
    public virtual DbSet<VAS_PROYECT> VAS_PROYECT { get; set; }
    public virtual DbSet<VAS_PROYECT_VULNERABILITY> VAS_PROYECT_VULNERABILITY { get; set; }
    public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP> VAS_PROYECT_VULNERABILITY_IP { get; set; }
    public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP_PORT> VAS_PROYECT_VULNERABILITY_IP_PORT { get; set; }
    public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP_URLS> VAS_PROYECT_VULNERABILITY_IP_URLS { get; set; }
    public virtual DbSet<VAS_VULNERABILITY> VAS_VULNERABILITY { get; set; }
    public virtual DbSet<VAS_VULNERABILITY_ENG> VAS_VULNERABILITY_ENG { get; set; }
    public virtual DbSet<VAS_VULNERABILITY_PARENT> VAS_VULNERABILITY_PARENT { get; set; }
    public virtual DbSet<VAS_VULNERABILITY_VULNERABILITY_PARENT> VAS_VULNERABILITY_VULNERABILITY_PARENT { get; set; }
}

I just don't know what else can be causing the error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

SOLVED:

The columns of the table in the database and the entity were different. I Re-Create the model with the right columns and now is working.