-1

i have a problem in adding entity to database in .net core.all of my code execute with no error but any record inserts in database this is my adding service below

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

any help will be highly appreciated

Serge
  • 40,935
  • 4
  • 18
  • 45
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Please see: [What Do You Mean “It Doesn't Work”?](https://meta.stackexchange.com/q/147616) – Progman May 30 '21 at 14:40
  • You aren't saving anything to the database. EF Core doesn't deal with connections and tables, it deals with *objects* , their mapping to tables and persisting *object* changes in batches. You never call `SaveChanges` so nothing is ever saved – Panagiotis Kanavos May 30 '21 at 16:23

1 Answers1

2

replace

 await _context.TableHpls.AddAsync(tableHpl);

with this

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

And I think you have to add some validation too:

 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;
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • thank you i dont know why i forgot savechanges() :) – reza setareh May 30 '21 at 14:42
  • due the method use async task, I suggest to use await _context.TableHpls.AddAsync(tableHpl); await _context.SaveChangesAsync(); – ariefs May 30 '21 at 15:07
  • thank you guys it works now.i have another question. suppose that user does not fill in the second textboxes for phone details i mean Hplphones[1] it will be inserted a full null record in database how can i control this issue in object initializer in code above thank you – reza setareh May 30 '21 at 15:19
  • @rezasetareh Pls post your TableHpl class too. After this I can only give you some advice – Serge May 30 '21 at 16:08
  • @Serge thank you i used your code in another way – reza setareh May 31 '21 at 13:25