У меня есть функция, которая читает данные из файла Excel. Моя цель - вставить в базу данных эти данные. Я уже определил консольное приложение и уровень сервиса. Нет проблем с получением списка ReportCard. Вставляя ReportCardDto в базу данных, я получил это сообщение об ошибке: «Недопустимое значение для ключа». Но я не могу найти свою ошибку.
class Program
{
static void Main(string[] args)
{
Run();
}
private static void Run()
{
try
{
using (var bootstrapper = AbpBootstrapper.Create<PkdsExcelWorksModule>())
{
bootstrapper.Initialize();
using (var reader = bootstrapper.IocManager.ResolveAsDisposable<ReportCardExcelWorks>())
{
List <ReportCardDto> list = reader.Object.GetReportCardDatas(@"C:\Users\is97339\source\repos\PKDS\src\Softtech.Pkds.ExcelWorks\ReportCard\data\pkds_veriler_V2.xlsx", "Sheet1");
using (var deneme = bootstrapper.IocManager.ResolveAsDisposable<ReportCardService>())
{
deneme.Object.InsertReportCardListToDb(list);
Console.WriteLine("İslem Tamamlandi");
}
}
}
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
[DependsOn(typeof(PkdsApplicationModule), typeof(PkdsCoreModule), typeof(PkdsEntityFrameworkModule))]
public class PkdsExcelWorksModule: AbpModule
{
public PkdsExcelWorksModule(PkdsEntityFrameworkModule abpProjectNameEntityFrameworkModule)
{
abpProjectNameEntityFrameworkModule.SkipDbSeed = true;
}
public override void PreInitialize()
{
}
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(PkdsExcelWorksModule).GetAssembly());
ServiceCollectionRegistrar.Register(IocManager);
}
}
public List<ReportCardDto> GetReportCardDatas(string path, string sheetName)
{
XSSFWorkbook xssfwb;
Console.WriteLine("Reading Excel File!");
DataFormatter df = new DataFormatter();
List<ReportCardDto> reportCardDtoList = new List<ReportCardDto>();
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
}
ISheet sheet = xssfwb.GetSheet(sheetName);
for (int row = 0; row <= sheet.LastRowNum; row++)
{
if (row == 0)
{
if (sheet.GetRow(row) == null) //empty sheet
{
Console.WriteLine("Sheet is empty");
}
}
else if (sheet.GetRow(row) != null) //null is when the row only contains empty cells
{
ReportCardDto reportCardDto = new ReportCardDto
{
DirectorateCode = Convert.ToInt32(sheet.GetRow(row).GetCell(5).NumericCellValue),
DirectorateName = sheet.GetRow(row).GetCell(0).StringCellValue,
Note = (float)sheet.GetRow(row).GetCell(4).NumericCellValue,
Quarter = Convert.ToInt32(sheet.GetRow(row).GetCell(2).NumericCellValue),
Target = sheet.GetRow(row).GetCell(1).StringCellValue,
TargetCode = sheet.GetRow(row).GetCell(6).StringCellValue,
Year = (int)sheet.GetRow(row).GetCell(3).NumericCellValue
};
reportCardDtoList.Add(reportCardDto);
}
}
return reportCardDtoList;
}
[DependsOn(
typeof(PkdsCoreModule),
typeof(AbpAutoMapperModule))]
public class PkdsApplicationModule : AbpModule
{
public override void PreInitialize()
{
Configuration.Authorization.Providers.Add<PkdsAuthorizationProvider>();
}
public override void Initialize()
{
var thisAssembly = typeof(PkdsApplicationModule).GetAssembly();
IocManager.RegisterAssemblyByConvention(thisAssembly);
Configuration.Modules.AbpAutoMapper().Configurators.Add(
// Scan the assembly for classes which inherit from AutoMapper.Profile
cfg => cfg.AddProfiles(thisAssembly)
);
}
}
public class ReportCardService : PkdsAppServiceBase, IReportCardService
{
private IRepository<ReportCard> _reportCardRepository;
public ReportCardService(IRepository<ReportCard> reportCardRepository)
{
_reportCardRepository = reportCardRepository;
}
public void InsertReportCardListToDb(List<ReportCardDto> reportCardDtoList)
{
foreach (ReportCardDto item in reportCardDtoList)
{
var reportCard = Mapper.Map<ReportCard>(item);
_reportCardRepository.InsertAsync(reportCard);
}
}
}