SHARE
TWEET

Untitled

a guest Sep 16th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using LinqToExcel;
  2. using System.Threading.Tasks;
  3.  
  4. namespace AbiliticsTask
  5. {
  6.     class Startup
  7.     {
  8.         static async Task Main(string[] args)
  9.         {
  10.             using (var dbContext = new ApplicationDbContext())
  11.             {
  12.                 dbContext.Database.EnsureCreated();
  13.  
  14.                 var RowNum = 2;
  15.                 var excel = new ExcelQueryFactory(@"Nobel Prize Winners.xlsx");
  16.                 excel.AddMapping<NobelWinner>(x => x.BirthPlace, "Birth Place");
  17.                 excel.AddMapping<NobelWinner>(x => x.PrizeName, "Prize Name");
  18.                 excel.AddMapping<NobelWinner>(x => x.FieldOrLanguage, "Field/Language");
  19.  
  20.                 var ws = excel.Worksheet<NobelWinner>("nobel");
  21.                 foreach (var row in ws)
  22.                 {
  23.                     row.Id = RowNum;
  24.                     var entity = await dbContext.NobelWinners.FindAsync(RowNum);
  25.                     var alreadyExist = entity != null;
  26.                     if (alreadyExist)
  27.                     {
  28.                         entity.Year = row.Year;
  29.                         entity.Category = row.Category;
  30.                         entity.Name = row.Name;
  31.                         entity.Birthdate = row.Birthdate;
  32.                         entity.BirthPlace = row.BirthPlace;
  33.                         entity.County = row.County;
  34.                         entity.Residence = row.Residence;
  35.                         entity.FieldOrLanguage = row.FieldOrLanguage;
  36.                         entity.PrizeName = row.PrizeName;
  37.                         entity.Motivation = row.Motivation;
  38.                     }
  39.                     else
  40.                     {
  41.                        await dbContext.NobelWinners.AddAsync(row);
  42.                     }
  43.                     await dbContext.SaveChangesAsync();
  44.                     RowNum++;
  45.                 }
  46.             }
  47.  
  48.  
  49.  
  50.         }
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top