How to configure DbContext for the name of tables in the singular?

1

I am starting to work with MVC 5 and Entity Framework 6, I clarify in advance that in my project I am not using visual studio assistants for EF since I want to understand in depth how it works. Well, my problem comes when reading a table from my database.

In SQL Server I have my structure as follows:

TABLE [dbo].[Usuario](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nombre] [varchar](100) NULL,
    [Apellidos] [varchar](100) NULL,
    [TipoAreaId] [int] NULL,
    [Email] [varchar](100) NULL,
    [Password] [varchar](50) NULL,
    [Status] [tinyint] NULL,

In Visual Studio I created my class:

public class Usuario
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Nombre { get; set; }
        public string Apellidos { get; set; }
        public int TipoAreaId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public EStatus Status { get; set; }
        public enum EStatus { Eliminado = 0, Activo = 1 }

    }

Now my EntityFramework DbContext is the following:

public class AlbatrosDB: DbContext
    {
        public AlbatrosDB() : base("AlbatrosDB")
        {Database.SetInitializer<AlbatrosDB>(null);}

        public DbSet<Usuario> UsuarioDB { get; set; }
    }

In my controller, the LINQ query is like this:

[HttpPost]
        public ActionResult Login(Login mdl)
        {
            AlbatrosDB conn = new AlbatrosDB();

            var Usuario = conn.UsuarioDB.SingleOrDefault(dbo => dbo.Email == mdl.Email 
            && dbo.Password == mdl.Password
            && dbo.Status == Models.Administracion.Usuario.EStatus.Activo);

            if (Usuario != null)
            {
                MostrarMensaje(Mensaje.Tipo.Correcto, "Login Correcto");
                return View("Index");
            }
            else
            {
                MostrarMensaje(Mensaje.Tipo.Alerta, "Datos de inicio de sesión no válidos.");
                return View("Index");
            }
        }

When I run my application I get the following error:

  

Server error in the application '/'. Invalid object name   'dbo.Users'.

     

Description: Unhandled exception when executing the   Current Web request. Check the stack tracking to get   more information about the error and where it originated in the code.

     

Exception details: System.Data.SqlClient.SqlException: Invalid   object name 'dbo.Users'.

     

Source code error:

     

Line 24: AlbatrosDB conn = new AlbatrosDB (); Line 25:   Line 26: var User = conn.UsuarioDB.SingleOrDefault (dbo   = > dbo.Email == mdl.Email Line 27: & & dbo.Password == mdl.Password Line 28: & amp; & dbo.Status ==   Models.Administracion.Usuario.EStatus.Activo);

In the error I realize that the Entity Framework is adding an 's' to the name of my table, which is false, in some forums I was reading that it is a configuration of the name of the tables (In singular or plural), however I have not found information about how I can do this configuration manually.

I hope you can help me friends and thank you in advance.

    
asked by Ivan.Enriquez 21.01.2018 в 19:16
source

1 answer

0

By default, EF pluralizes the names of the table, that is why the error. In your class Usuario add the following:

[Table("Usuario")]
public class Usuario

Or using Fluent Api in your context AlbatrosDB :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Usuario>()
  .ToTable("Usuario");
}
    
answered by 21.01.2018 / 23:07
source