本文介绍了EF代码第一个无效列名称“CategoryCategoryID”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个名为Northwind的现有数据库与Webform应用程序。
运行应用程序时会产生错误:
'无效的列名称CategoryCategoryID'。
任何一个帮助我?
提前感谢!
There is a existing database named Northwind with a webform application.when running the application raise up a error:'invalid column name CategoryCategoryID'.any one help me?.thanks in advance!!
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; }
}
Product.cs:
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; }
}
Products.aspx
protected void Page_Load(object sender, EventArgs e)
{
Northwind northwind = new Northwind();
var products = from p in northwind.Products
where p.Discontinued == false
select p;
GridView1.DataSource = products.ToList();
GridView1.DataBind();
}
推荐答案
在您的产品
实体中添加新的FK属性:
One way to solve this is to add a new FK property to your Product
entity:
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; }
}
这篇关于EF代码第一个无效列名称“CategoryCategoryID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!