2012-11-06 43 views
1

我有有类别1和类别2字段指示类别表ID(FK)产品表格中,实体框架,变换实体

和每个字段包含一个数字(ID)。

我想为设置的类别名称添加实体时,我检索数据,但我不知道如何使category1_name和category2_name可以有类别名称。 (很难解释= 3)

EX)

public class Product 
{ 
    [Key] 
    public int no {get; set;} 
    public int category1 {get; set;} // category table ID number 
    public int category2 { get; set;} // category table ID number 

    public virtual Category category1_name {get; set;} 
    public virtual Category category2_name {get; set;} 
} 


public class Category 
{ 
    [Key] 
    public int no {get; set;} 
    public string category {get; set;} 
} 

但是,当我尝试检索数据,我得到了一个错误,

未知列 'Extent1.category1_name_no' 在“on子句'

如何将category1_name映射到类别表ID?

+0

你可能想看看ForeignKeyAttribute。 – Pawel

回答

4

您如何期望EF知道哪个标量属性映射到哪个导航属性。您必须使用流畅的API或数据注释来配置正确的映射。

public class Product 
{ 
    [Key] 
    public int no {get; set;} 
    public int category1 {get; set;} // category table ID number 
    public int category2 { get; set;} // category table ID number 

    [ForeignKey("category1")] 
    public virtual Category category1_name {get; set;} 

    [ForeignKey("category2")] 
    public virtual Category category2_name {get; set;} 
} 

您应该考虑遵循命名属性的正确命名约定。