Ef model composite foreign key

Can any one help me how to create composite foreign key in a ef model
The scenario
public class Item
{
[Key]
public int Id { get; set; }

public string Name { get; set; }

[ForeignKey("Category")]
public int? CategoryId { get; set; }

public virtual Category Category { get; set; }

public int? SubCategoryId { get; set; }   

// How to define composite foreign key to this property (CategoryId , SubCategoryId )

public virtual SubCategory SubCategory { get; set; }

}

public class Category
{
[Key]
public int Id { get; set; }

public string Name { get; set; }

}

public class Category
{
[Key]
public int Id { get; set; }

[ForeignKey("Category")]
public int CategoryId { get; set; }

public virtual Category Category { get; set; }

public string Name { get; set; }

}

Sample Tables

Category:
Id Name

1 catg1
2 catg2
3 catg3

Sub Category:
Id CategoryId Name

1 1 SubCatg1
2 1 SubCatg2
3 1 SubCatg3
1 2 SubCatg11
2 2 SubCatg21
1 3 SubCatg111
2 3 SubCatg211
3 3 SubCatg111
4 3 SubCatg211

Item:
Id CategoryId SubCategoryId Name

1 1 2 aaaaa
2 1 null bbb
3 null null ccc
4 3 4 dd

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.