2011-02-20 26 views
12

Existe una base de datos denominada Northwind con una aplicación webform. al ejecutar la aplicación, genera un error: 'nombre de columna no válida CategoryCategoryID'. alguien me ayude ?. gracias de antemano!Código EF primer nombre de columna no válido "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; } 
} 

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

qué te comento Id? Probablemente el esquema no coincide – paragy

Respuesta

13

Una manera de resolver esto es agregar una nueva propiedad FK a su entidad Product:

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; } 
} 
+5

¿Por qué sucede esto? –

+0

ForeignKey está en System.ComponentModel.DataAnnotations; espacio de nombres y también se puede poner en [ForeignKey ("Categoría")] público virtual Categoría Categoría {get; conjunto; } –

+0

¿Qué tal si es opcional? – Shimmy

Cuestiones relacionadas