EF Code First – Model
I have 2 classes:
public class Alias
{
public int id { set; get; }
public string Name { set; get; }
public ICollection<Tweet> Tweets { set; get; }
}
public class Tweet
{
public int id {set;get;}
public Alias Author { set; get; }
}
It creates a Author_Id (FK) in the Tweet class. Okay makes sense, because it has a relationship between two models. But how come it doesn’t create Tweet_Id (FK) in the Alias class? or is it because I am using ICollection? why it creates a FK in Tweet class and not vice versa?
What keyword tells the EF to create a FK in its model
Also I tried the below code and it fails….
public class Alias
{
public int id { set; get; }
public string Name { set; get; }
public Tweet Tweets { set; get; }
}
public class Tweet
{
public int id {set;get;}
public Alias Author { set; get; }
}
I am trying to understand how the model works, how and when it creates a FK? … please provide me a brief explanation… Thank you in advance!
SQL doesn’t need a foreign key on both tables. There isn’t a concept of a direction the way there is in C#.
The Tweet table needs an id to say which Alias it’s connected to. From that you can query all tweets that were made by that alias pretty easily.
If there were a Tweet_Id on the Alias table, what would be set to once there was a second tweet made by the same person?