"CategoryCategoryID" is an incorrect column name in the EF Code.

ef-code-first entity-framework

Question

A webform application and a database called Northwind already exist. A message reading "invalid column name CategoryCategoryID" appears while the program is executing. Can somebody assist me? In advance, many thanks!

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();
}
1
13
2/20/2011 4:23:35 PM

Accepted Answer

To resolve this, one option is to include a new FK attribute in yourProduct 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; }
}
13
7/28/2011 4:44:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow