微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 .net core 中添加实体时出现问题

如何解决在 .net core 中添加实体时出现问题

我在将实体添加到 .net core 中的数据库时遇到问题。我的所有代码都没有错误地执行,但是在数据库中插入了任何记录,这是我在下面的添加服务

        public async Task<int> AddHpl(IFormFile ClinicImgUp,AddHealthPlaceviewmodel addHealthPlaceviewmodel)
    {
        TableHpl tableHpl = new TableHpl
        {
            TabloTitle = addHealthPlaceviewmodel.TabloTitle,Address = addHealthPlaceviewmodel.Address,Services = addHealthPlaceviewmodel.Services,Others = addHealthPlaceviewmodel.Others,Personels = addHealthPlaceviewmodel.Personels,CityCode = addHealthPlaceviewmodel.CityCode,Recid = addHealthPlaceviewmodel.Recid,Telegram = addHealthPlaceviewmodel.Telegram,Email = addHealthPlaceviewmodel.Email,Website = addHealthPlaceviewmodel.Website,Instagram = addHealthPlaceviewmodel.Instagram,PlaceCode = addHealthPlaceviewmodel.PlaceCode,TableHplphones = new List<TableHplphone>
            {
                new TableHplphone
                {
                    Mobile = addHealthPlaceviewmodel.Hplphones[0].Mobile,OfficePhone = addHealthPlaceviewmodel.Hplphones[0].OfficePhone
                },new TableHplphone
                {
                    Mobile = addHealthPlaceviewmodel.Hplphones[1].Mobile,OfficePhone = addHealthPlaceviewmodel.Hplphones[1].OfficePhone
                }
            }
        };
        if (ClinicImgUp != null && ClinicImgUp.IsImage())
        {

            tableHpl.OfficePic = NameGenerator.GenerateUniqCode() + Path.GetExtension(ClinicImgUp.FileName);
            string imagePath = Path.Combine(Directory.GetCurrentDirectory(),"wwwroot/img",tableHpl.OfficePic);
            await using var stream = new FileStream(imagePath,FileMode.Create);
            ImageResizer.ResizeImage(ClinicImgUp,stream);
        }

        await _context.TableHpls.AddAsync(tableHpl);
        return tableHpl.Hplid;
    }

任何帮助将不胜感激

解决方法

替换

 await _context.TableHpls.AddAsync(tableHpl);

有了这个

 _context.TableHpls.Add(tableHpl);
await _context.SaveChangesAsync();

而且我认为您还必须添加一些验证:

 TableHpl tableHpl = new TableHpl
        {
            TabloTitle = addHealthPlaceViewModel.TabloTitle,Address = addHealthPlaceViewModel.Address,Services = addHealthPlaceViewModel.Services,Others = addHealthPlaceViewModel.Others,Personels = addHealthPlaceViewModel.Personels,CityCode = addHealthPlaceViewModel.CityCode,Recid = addHealthPlaceViewModel.Recid,Telegram = addHealthPlaceViewModel.Telegram,Email = addHealthPlaceViewModel.Email,Website = addHealthPlaceViewModel.Website,Instagram = addHealthPlaceViewModel.Instagram,PlaceCode = addHealthPlaceViewModel.PlaceCode
        };
if (addHealthPlaceViewModel.Hplphones!=null &&
     addHealthPlaceViewModel.Hplphones.Count > 0
{
 var tableHplphones = new List<TableHplphone>();

   foreach(var phone in addHealthPlaceViewModel.Hplphones)
     {
     
             tableHplphones.Add(  new TableHplphone
                {
                    Mobile = item.Mobile,OfficePhone = item.OfficePhone
                }
           );
      }

tableHpl.TableHplphones=tableHplphones;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。