2011-02-20 14 views
12

Esiste un database esistente denominato Northwind con un'applicazione Webform. durante l'esecuzione dell'applicazione genera un errore: 'nome colonna non valido CategoriaCategoryID'. qualcuno mi aiuti ?. grazie in anticipo !!Nome prima colonna non valido Nome EF "CategoriaCategoryID"

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

perché si commenta Id? probabilmente lo schema mis match – paragy

risposta

13

Un modo per risolvere questo è quello di aggiungere una nuova proprietà FK al Product entità:

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

Perché questo succede ??? –

+0

ForeignKey è in System.ComponentModel.DataAnnotations; namespace e può anche essere inserito in [ForeignKey ("Category")] public virtual Categoria Category {get; impostato; } –

+0

Che ne dici di renderlo opzionale? – Shimmy

Problemi correlati