1

I have Product table that have category1 and category2 field,

and each field contain a number(ID) that indicate Category Table ID (FK).

and I want to add entity for set category name when I retrieve data, but I don't know how to make category1_name and category2_name can have category name. (very hard to explain =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;}
}

But when I try to retrieve data, I got an error,

Unknown column 'Extent1.category1_name_no' in 'on clause'

How can I mapping the category1_name to category table ID?

1
  • You probably want to take a look at the ForeignKeyAttribute. Commented Nov 6, 2012 at 4:31

1 Answer 1

3

How would you expect EF to know which scalar property to map to which navigational property. You have to either use the fluent API or data annotations to configure the correct mappings.

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

You should consider following proper naming convention for naming properties.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.