Using Entity Framework with no defined keys

56 Views Asked by At

I am having problems with the keys when trying to make my controllers, I was reading some of the already questions posted in here, and I found that I needed to explicitly define the key in the variable, and I did so.. but the problem still persist and I don't know how to fix it.

Error is something like this:

"Entity Type 'Product' has no key defined, EntitySet 'Products' has no keys defined"

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MVC_Catalogo.Models
{
  public class Product
{

    private String descripcion { get; set; }
    private String barras { get; set; }
    private String alterno { get; set; }
    private double precioc { get; set; }
    private double preciop { get; set; }
    private String imagen { get; set; }
    [Key]
    private Guid productoId { get; set; }
    private Guid idproveedor { get; set; }
    private int activo { get; set; }

    public class JomDBContext : DbContext
    {
        public DbSet<Product> Products { get; set; }

    }


}
}
1

There are 1 best solutions below

0
On BEST ANSWER

Answer:

I'm not sure if any of these are the issue, but you might try: 1. making the properties public instead of private. 2. matching up the class name with the ID field. So change productoId to ProductId. 3. Annotating the ID field with [DatabaseGenerated(DatabaseGeneratedOption.Identity] as well as [Key]