在 .net core 中添加实体时出现问题
there is a problem adding entity in .net core
我在 .net 中向数据库添加实体时遇到问题 core.all 我的代码执行时没有错误,但是数据库中的任何记录插入这是我下面的添加服务
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;
}
我在 .net 中向数据库添加实体时遇到问题 core.all 我的代码执行时没有错误,但是数据库中的任何记录插入这是我下面的添加服务
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;
}