如何使用Entity Framework更新现有行?

时间:2017-11-06 02:29:19

标签: asp.net-mvc entity-framework c#-4.0 dependency-injection linq-to-entities

我有以下一对多表,如下所示。在使用以下代码编辑现有行时遇到问题。请理解我对ET关系很新,所以任何详细的解释都将不胜感激。为什么它返回空值?

enter image description here

public void UpdateReportGroup(TReportHeaderModel model)
    {

        if (model.THeaderTitle == null)
        {
            throw new Exception("Report Group Title must be filled in");

        }

        if (model.THeaderTitle.Length <= 0)
        {
            throw new Exception("A Report Group Title must be filled in.");

        }
        using (var connection = new TReportEntitiesConnection())
        {

            var header = connection.THeaders.SingleOrDefault(f => f.ID == model.ID);
            var reports = connection.TReport.Where(f => f.THeaderID == model.ID);
            connection.TReport.RemoveRange(reports);
            foreach (var urls in model.TReports)
            {
                connection.TReport.Add(new TReport()

                {
                    TReportName = urls.name,
                    URL = urls.url,
                });
            }
            connection.THeaders.Add(header);
            connection.SaveChanges()      

        }
    }

每次,我调试它,它为&#39; TReport&#39;提供空值。表。 我创建的新行与以下代码完美配合。意思是,我正在使用正确的字段名称返回正确的表单。

public void CreateReport(TReportHeaderModel model)
        {


            if (model.THeaderTitle == null)
            {
                throw new Exception("Report Group Title must be filled in");

            }

            if (model.THeaderTitle.Length <= 0)
            {

                throw new Exception("A Report Group Title must be filled in.");

            }

                using (var connection = new TReportEntitiesConnection())

                {
                    var header = new THeader()
                    {
                        ClientID = model.ClientID,

                        THeaderTitle = model.THeaderTitle,
                        RowNumber = model.RowNumber
                    };


                    foreach (var urls in model.TReports)
                    {

                        header.TReports.Add(new TReport()
                        {

                            TReportName = urls.name,
                            URL = urls.url


                        });

                    }

                    connection.THeaders.Add(header);
                    connection.SaveChanges();

                }

        }

如您所见,我正在关注DI模式,因此我在控制器中调用这两种方法如下:

[HttpPost]
        public ActionResultModel CreateReportAPI([FromBody] TReportHeaderModel model) //attempt 3
        {

            try {
                if (ModelState.IsValid)
                {
                    var isValid = _tReportingService.HeadernameExists(model.THeaderTitle);

                    if (!isValid)

                    {
                        Console.WriteLine("it does not exist");
                        var user = this.GetCurrentUserAccount();
                        model.ClientID = user.SelectedClient.ID;

                        _tReportingService.CreateReport(model);

                    }

                    else //Update method comes till here and it goes //straight to the error
                    {

                        Console.WriteLine("it exists");
                        var user = this.GetCurrentUserAccount();
                        model.ClientID = user.SelectedClient.ID;

                        _tReportingService.UpdateReportGroup(model);

                    }
                }


                return new ActionResultModel()
                {

                    Success=true, 
                    Message="Report Group Successfully Saved."


                };




            }

1 个答案:

答案 0 :(得分:0)

我想这将是我最后一次在这里发帖提问,因为我明白如果你只是问这个问题我就不会这么做,我宁愿研究更多并继续尝试,直到我得到它,所以我在这里回答我自己的问题,因为我解决了它。

当模型从与ET完全分离的WebApi生成时,我首先加载数据库并比较已添加,删除和删除的子项。更新。这是一个完美的一对多关系的更新/删除的例子。

      energy    fat
1      2000      28
2      1900      17
3      2200      30
4      1750      15
5      1800      18
6      1600      12