0

estoy tratando de llenar un formulario con información de una base de datos. creo un objeto

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace formularioCore.Models
{
    public class Curso
    {
        [Key]
        public int Id { get; set; }
        [Required]
        [Display(Name = "Nombre curso")]
        public String NombreCurso { get; set; }
        [Display(Name = "Cantidad de clases")]
        public int Cantidadclases { get; set; }
        public int Precio { get; set; }
    }
}

mi conexión JSON a la base de datos:

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=Curso;Trusted_Connection=True;MultipleActiveResultSets=True"

  },

y en Startup.cs

  services.AddDbContext<ApplicationDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

y el modelo

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace formularioCore.Models
{
    public class ApplicationDBContext : DbContext

    {

        public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base (options)
            {

            }

    }
}

bien, posteriormente por la consola meto Add-Migration crearTabla Lo hace bien sin errores, y me crea la carpeta de migrations pero me la crea con los metodos vacios y a la hora de ejecutar el guardar o el traer datos de la base de datos me dice

nvalidOperationException: The entity type 'Curso' was not found. Ensure that the entity type has been added to the model.

Alguna idea? muchas gracias.

1 respuesta 1

2

Te falto agregar el DbSet de Curso a tu BbContext:

public class ApplicationDBContext : DbContext
{
    public DbSet<Curso> Cursos{ get; set; }
    public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base (options)
        {

        }

}

Con esto ya debería crear las tablas y generar código en el UP y down de la migración

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.