Foreign key annotations in MVC

I have two tables:

State(StateID int,StateName string)

City(CityID int,StateID int,CityName string)

I am working on MVC4 with the first code approach. The code I use for State and City . Model -

For condition:

[Key]

public int StateID{get;set;}

public string StateName{get;set;}

For the city:

[Key]
public int CityID{get;set;}


[ForeignKey("State")]
public string StateID{get;set;}
public virtual State State{get;set;}

This code creates a foreign key for the StateID from the state table. All I need works, but since I'm new to MVC, I got the following code, does the same thing too.

[ForeignKey("StateID")]
public string Stateid{get;set;}
public virtual State StateID{get;set;}

, ... ForeignKey Annotation, State, Property ie StateID , . . .

+3
1

ForeignKey , .

, :

[ForeignKey("State")]
public string StateID { get; set; }

public virtual State State { get; set; } // This is your navigation property

. "State", , .

StateID, [ForeignKey("StateID")] .

MSDN .

+7

All Articles