0

I am having trouble to see what i need to correct or add to my controller in order to have my data saved to the database. See below what i have so far.

Create method

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

Table Relationships

-Team one-to-one TeamContact
-TeamContact one-to-on TeamAddress

IDataSource Interface

    IQueryable<Team> Teams { get; }        
    IQueryable<TeamAddress> TeamAddresses { get; }
    IQueryable<TeamContact> TeamContacts { get; }
    void Save();

Context class

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

What am i missing to have my data saved to the database?

svick
  • 236,525
  • 50
  • 385
  • 514
Komengem
  • 3,662
  • 7
  • 33
  • 57
  • I don't see how you add your new entity to the context - something like `_dataSource.Teams.Add(team)` – Pawel Nov 28 '12 at 05:05
  • @Pawel `Add()` is a method of ICollection and would work if entity Team had an Icollection property in another entity. eg: `public virtual ICollection Team{ get; set; }` Your suggestion will work when i write a create method for the Player entity which is being collected by the team entity. – Komengem Nov 28 '12 at 05:34
  • 2
    I am talking about `Add()` exposed on `DbSet Teams` - if you don't add this how DbContext should know about this object? The only other way would be to add an entity that references the new Team entity you created but I don't see anything like that in your code. – Pawel Nov 28 '12 at 05:37
  • @Pawel Thanks, wish there was a cheat sheet. Anyways, i have a difficulty adding that method unless i change my repository pattern. Any ideas on how i could add this without having to change much? This is the only pattern i know that works with auto migration – Komengem Nov 29 '12 at 02:22

1 Answers1

1

You don't have any code in your controller that is actually adding the Team to your database. Right now you are simply creating a new Team object in memory and saving changes to your database. Nothing is actually being added to the database to save.

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();
HTX9
  • 1,717
  • 3
  • 15
  • 27
  • I tried that. `Add()` is a method of ICollection and would work if entity Team had an Icollection property in another entity. eg: `public virtual ICollection Team{ get; set; }` Your suggestion will work when i write a create method for the Player entity which is being collected by the team entity – Komengem Nov 28 '12 at 05:38
  • @KomengeMwandila `Add()` is also a method of `DbSet`. See [the MSDN documentation here](http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.add(v=vs.103).aspx). – HTX9 Nov 28 '12 at 05:39
  • Any ideas on how i would implement that little section. i have had no luck with what i have tried, unless i change my repository setup?? – Komengem Nov 29 '12 at 02:17
  • I think your problem was you were trying to use the `Add()` method on the `IQueryable` when you need to use it on the `DbSet`. You may need to change your repository set up because I'm not exactly sure what you are trying to use the `IQueryable` properties for. – HTX9 Nov 29 '12 at 02:38
  • Sorry i took so long, but i made it work. Had to restructure my repository and also had to write separate save method for a set of entities which also included an add method. See the edit in your initial answer post – Komengem Dec 05 '12 at 00:39