use*_*222 13 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; }
public string Description { get; set; }
public byte[] Picture { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
Northwind.cs
public class Northwind: DbContext
{
public DbSet< Product > Products { get; set; }
public DbSet< Category > Categorys{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
Mor*_*avi 13
解决此问题的一种方法是向您的Product实体添加新的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; }
}
Run Code Online (Sandbox Code Playgroud)