views:

28

answers:

1

Hi there

I have the following code:

//
        // POST: /PlayRoundHole/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                DB db = new DB();

                PlayRound playRound = new PlayRound();
                playRound.PlayerID = Int64.Parse(Request.Form["Value"]);
                playRound.TenantID = 1;
                playRound.RoundID = Int64.Parse(Request.Form["RoundID"].ToString());
                playRound.Score = 0;

                var playRoundHoles = from prh in db.PlayRoundHoles.ToList()
                                     from hl in db.Holes.ToList()
                                     where prh.HoleID == hl.HoleID
                                     where prh.PlayRoundID == Int64.Parse(Request.Form["RoundID"].ToString())
                                     select new { prh.HoleID, hl.Sequence };
                foreach(var a in playRoundHoles)
                {
                    PlayRoundHole playRoundHole = new PlayRoundHole();
                    playRoundHole.HoleID = a.HoleID;
                    playRoundHole.Stroke = Byte.Parse(Request.Form["PlayRoundHoleID_" + a.Sequence].ToString());
                    playRound.PlayRoundHoles.Add(playRoundHole);
                }
                db.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

There is no error at all BUT I coun't see the data that is being saved. Any ideas? Is there anyway to trace this?

+1  A: 

Nothing will be saved because your new objects are not linked to the datacontext (db). If you want the new PlayRound instance and its PlayRoundHole children to be persisted to the database, you need to call the following:

db.PlayRounds.InsertOnSubmit(playRound);
db.SubmitChanges();

On a side note, the following query is fetching all PlayRoundHoles and all Holes from the database and then filters them in your application.

var playRoundHoles = from prh in db.PlayRoundHoles.ToList()
                     from hl in db.Holes.ToList()
                     where prh.HoleID == hl.HoleID
                     where prh.PlayRoundID == 42
                     select new { prh.HoleID, hl.Sequence };

What you probably wanted is something like:

var playRoundHoles = from prh in db.PlayRoundHoles
                     from hl in db.Holes
                     where prh.HoleID == hl.HoleID
                     where prh.PlayRoundID == 42
                     select new { prh.HoleID, hl.Sequence };

This will only fetch the matching hole and instead of fetching full rows from both tables, it will only fetch the HoleID and Sequence number.

Marcel Gosselin
Thanks for this. I fixed this already. But then when I looked the data ... the reference of PlayRoundID is filled blank in the database. I though this Add() somehow smart enough to put the id in. So how do I achieve this?
dewacorp.alliances
Never mind silly me. One of my table the identity has not been set. :)
dewacorp.alliances