2011-02-20 4 views
12

Il existe une base de données existante appelée Northwind avec une application Webform. lors de l'exécution de l'application déclencher une erreur: 'nom de colonne non valide CategoryCategoryID'. quelqu'un m'aider ?. merci d'avance !!Code EF premier nom de colonne non valide "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

pourquoi vous avez dit Id? probablement schema mis match – paragy

Répondre

13

Une façon de résoudre ce problème est d'ajouter une nouvelle propriété FK à votre entité 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

Pourquoi cela arrive-t-il ??? –

+0

ForeignKey est dans System.ComponentModel.DataAnnotations; namespace et peut également être mis sur le [ForeignKey ("Category")] public virtual Catégorie Catégorie {get; ensemble; } –

+0

Que diriez-vous de le rendre facultatif? – Shimmy

Questions connexes