开发者

Can't see the save data with linq?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜