I have 2 classes like below.
public class Quest
{
public int ID{ get;set; }
public string Objective
{
get;
set;
}
public DateTime StartDate
{
get;
set;
}
public string Story
{
get;
set;
}
public virtual QuestLocation Location
{
get;
set;
}
}
public class QuestLocation
{
public DateTime CreateDate
{
get;
set;
}
public int ID
{
get;
set;
}
public double Latitude
{
get;
set;
}
public double Longitude
{
get;
set;
}
public virtual Quest Quest
{
get;
set;
}
}
I created a new instance of Quest and after that I have set the location property of Quest simply creating a new instance of QuestLocation class.
Question q = new Question();
q.StartDate = DateTime.Now;
q.Objective = "foo";
q.Location = new QuestLocation(){ CreateDate = DateTime.Now, Latitude = 10 , Longitude = 10 };
And add newly created Quest into Database :
DataContext.Quests.Add(q);
DataContext.SaveChanges();
When I look at my database, Every object that i have initialized so far is created. However the foreign Key “Quest_ID” of QuestLocation remained NULL. How can I come up with this problem?
1 Answer