在没有定义键的情况下使用 Entity Framework
Using Entity Framework with no defined keys
我在尝试制作我的控制器时遇到键问题,我正在阅读此处发布的一些已经存在的问题,我发现我需要在变量中明确定义键,我这样做了..但问题依然存在,我不知道如何解决。
错误是这样的:
"Entity Type 'Product' has no key defined, EntitySet 'Products' has no keys defined"
代码:
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. 将属性设置为 public 而不是私有。 2. 将 class 名称与 ID 字段匹配。因此,将 productoId 更改为 ProductId。 3.用[DatabaseGenerated(DatabaseGeneratedOption.Identity]和[Key]
注释ID字段
我在尝试制作我的控制器时遇到键问题,我正在阅读此处发布的一些已经存在的问题,我发现我需要在变量中明确定义键,我这样做了..但问题依然存在,我不知道如何解决。
错误是这样的:
"Entity Type 'Product' has no key defined, EntitySet 'Products' has no keys defined"
代码:
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. 将属性设置为 public 而不是私有。 2. 将 class 名称与 ID 字段匹配。因此,将 productoId 更改为 ProductId。 3.用[DatabaseGenerated(DatabaseGeneratedOption.Identity]和[Key]
注释ID字段