Entity framework EF Code first无效列名“;CategoryCategoryID“;

Entity framework EF Code first无效列名“;CategoryCategoryID“;,entity-framework,ef-code-first,Entity Framework,Ef Code First,有一个名为Northwind的现有数据库,带有一个webform应用程序。 运行应用程序时引发错误: “无效的列名CategoryCategoryID”。 有人帮我吗?。 提前谢谢 Category.cs: public class Category { public int CategoryID { get; set; } // public int ID { get; set; } public string CategoryName { get; set; }

有一个名为Northwind的现有数据库,带有一个webform应用程序。 运行应用程序时引发错误: “无效的列名CategoryCategoryID”。 有人帮我吗?。 提前谢谢

Category.cs:

public class Category
{

    public int CategoryID { get; set; }
   // public int ID { get; set; }
    public string CategoryName { get; set; }
    public string Description { get; set; }
    public byte[] Picture { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}
public class Product
{

    public int ProductID { get; set; }
    //public int ID { get; set; }
    public string ProductName { get; set; }
    public Decimal? UnitPrice { get; set; }
    public bool Discontinued{ get; set; }
    public virtual Category Category{ get; set; }
}
Northwind.cs

public class Northwind:   DbContext
{
    public DbSet< Product  > Products { get; set; }
    public DbSet< Category > Categorys{ get; set; }
}

解决此问题的一种方法是将新的FK属性添加到
产品
实体:

public class Product
{
    public int ProductID { get; set; }        
    public string ProductName { get; set; }
    public Decimal? UnitPrice { get; set; }
    public bool Discontinued { get; set; }
    [ForeignKey("Category")]
    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }
}

你为什么评论Id?架构不匹配ForeignKey可能位于System.ComponentModel.DataAnnotations中;名称空间,也可以放在[ForeignKey(“Category”)]公共虚拟类别{get;set;}上。让它成为可选的怎么样?@Morteza Manani如果我们只在Products类中使用FluentAPI的navigation属性,怎么能做到这一点?
public class Product
{
    public int ProductID { get; set; }        
    public string ProductName { get; set; }
    public Decimal? UnitPrice { get; set; }
    public bool Discontinued { get; set; }
    [ForeignKey("Category")]
    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }
}