13

有一个名为 Northwind 的现有数据库和一个 webform 应用程序。运行应用程序时出现错误:“无效的列名 CategoryCategoryID”。任何人帮助我?提前致谢!!

类别.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; }
}

产品.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; }
}

北风.cs

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

产品.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();
}
4

1 回答 1

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; }
}
于 2011-02-20T17:03:38.403 回答