0

我无法查看需要更正或添加到控制器的内容,以便将我的数据保存到数据库中。请参阅下面我到目前为止的内容。

创建方法

[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();
    }

将数据保存到数据库中我缺少什么?

4

1 回答 1

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();
于 2012-11-28T05:31:38.783 回答