我的 ASP.NET MVC 项目中有两个模型。在播种以包含测试数据时,我执行以下操作:
context.Dialogs.Add(new Dialog
{
Id = 1,
Chapter = 1,
Index = 0,
DialogColor = "default-color",
DialogText = "blah blah!",
Character = "none",
Transition = false,
Fade = true,
Timer = 0,
Actions = new List<Action>
{
new Action { ActionText = "--continue--", ActionLink = 1, Id=1 }
}
});
这将记录保存在 Dialog 表中,但未保存 Actions,我知道我可能可以先保存对话框,然后将 Action 添加到它,但我希望能够像上面那样将其全部内联添加?
对话模型:
public class Dialog
{
[Key]
public int Id { get; set; }
public int Chapter { get; set; }
public int Index { get; set; }
public string DialogColor { get; set; }
public string DialogText { get; set; }
public string Character { get; set; }
public bool Transition { get; set; }
public bool Fade { get; set; }
public int Timer { get; set; }
public virtual IEnumerable<Action> Actions { get; set; }
}
行动模式:
public class Action
{
[Key]
public int Id { get; set; }
public string ActionText { get; set; }
public int ActionLink { get; set; }
public Dialog Dialog { get; set; }
}