我无法看到我需要更正或添加到我的控制器以便将我的数据保存到数据库中。见下文我到目前为止。
创建方法
[HttpPost]
public ActionResult Create(Team model)
{
if (ModelState.IsValid)
{
new Team
{
Name = model.Name,
Division = model.Division,
Description = model.Description,
TeamContact = new TeamContact
{
EmailAddress = model.TeamContact.EmailAddress,
PhoneNumber = model.TeamContact.PhoneNumber,
TeamAddress = new TeamAddress
{
Box = model.TeamContact.TeamAddress.Box,
StreetName = model.TeamContact.TeamAddress.StreetName,
StreetNumber = model.TeamContact.TeamAddress.StreetNumber,
City = model.TeamContact.TeamAddress.City,
PostalCode = model.TeamContact.TeamAddress.PostalCode,
Province = model.TeamContact.TeamAddress.Province
}
}
};
_dataSource.Save();
}
return View(model);
表格关系
-Team one-to-one TeamContact
-TeamContact one-to-on TeamAddress
IDataSource接口
IQueryable<Team> Teams { get; }
IQueryable<TeamAddress> TeamAddresses { get; }
IQueryable<TeamContact> TeamContacts { get; }
void Save();
上下文类
public DbSet<Team> Teams { get; set; }
IQueryable<Team> IDataSource.Teams
{
get { return Teams; }
}
public DbSet<TeamAddress> TeamAddresses { get; set; }
IQueryable<TeamAddress> IDataSource.TeamAddresses
{
get { return TeamAddresses; }
}
public DbSet<TeamContact> TeamContacts { get; set; }
IQueryable<TeamContact> IDataSource.TeamContacts
{
get { return TeamContacts; }
}
public void Save()
{
SaveChanges();
}
将数据保存到数据库中我缺少什么?
答案 0 :(得分:1)
您的控制器中没有任何实际将Team
添加到数据库的代码。现在,您只需在内存中创建一个新的Team对象并保存对数据库的更改。实际上没有任何东西被添加到数据库中以保存。
Team team = new Team
{
.. the rest of your model building code here ..
};
_dataSource.Teams.Add(team); // This adds the Team entity to the database
_dataSource.Save();