本文整理汇总了C#中DBContext.BulkInsert方法的典型用法代码示例。如果您正苦于以下问题:C# DBContext.BulkInsert方法的具体用法?C# DBContext.BulkInsert怎么用?C# DBContext.BulkInsert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBContext
的用法示例。
在下文中一共展示了DBContext.BulkInsert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportCountriesAndNamesIntoDB
public void ImportCountriesAndNamesIntoDB()
{
var countryService = new CountryService();
var nameService = new NameService();
Trace.WriteLine("Create DB if not exists");
System.Data.Entity.Database.SetInitializer(new DBInitializer());
Trace.WriteLine("Start DB sync");
List<Country> countries = countryService.GetCountriesAsync().Result;
using (var dbContext = new DBContext())
{
dbContext.BulkInsert(countries);
dbContext.SaveChanges();
int totalCountries = countries.Count();
int countriesCount = 0;
Trace.WriteLine($"DB sync: {totalCountries} countries downloaded");
foreach (Country country in dbContext.Countries)
{
Trace.WriteLine($"DB sync: start country {country.Name} ({countriesCount++} of {totalCountries})");
BabyName[] names = nameService.GetNamesAsync(country.HRef).Result.ToArray();
foreach (BabyName name in names)
{
name.CountryId = country.Id;
}
using (var nameContext = new DBContext())
{
nameContext.BulkInsert(names);
nameContext.SaveChanges();
}
Trace.WriteLine($"DB sync: end country {country.Name}: {names.Count()} names downloaded");
}
}
Trace.WriteLine("End DB sync");
}
示例2: ImportStatesIntoDB
//.........这里部分代码省略.........
Points = "470,162,489,147",
});
states.Add(new State
{
CountryCode = "AS",
Pin = "13,145",
Offset = -11,
Points = "7,150,20,135",
});
states.Add(new State
{
CountryCode = "PW",
Pin = "437,115",
Offset = 9,
Points = "427,126,442,109",
});
states.Add(new State
{
CountryCode = "PN",
Pin = "69,160",
Offset = -8,
Points = "63,165,82,153",
});
states.Add(new State
{
CountryCode = "FM",
Pin = "470,115",
Offset = 11,
Points = "463,122,478,110",
});
states.Add(new State
{
CountryCode = "PG",
Pin = "454,138",
Offset = 10,
Points = "447,141,471,126",
});
states.Add(new State
{
CountryCode = "CK",
Pin = "28,154",
Offset = -10,
Points = "20,155,32,137",
});
states.Add(new State
{
CountryCode = "MP",
Pin = "452,104",
Offset = 10,
Points = "446,110,458,91",
});
states.Add(new State
{
CountryCode = "PF",
Pin = "42,149",
Offset = -10,
Points = "35,163,61,145",
});
states.Add(new State
{
CountryCode = "KI",
Pin = "490,123",
Offset = 12,
Points = "485,134,496,115",
});
states.Add(new State
{
CountryCode = "TO",
Pin = "7,154",
Offset = 13,
Points = "0,161,14,142",
});
states.Add(new State
{
CountryCode = "UM",
Pin = "481,98",
Offset = 12,
Points = "476,103,486,93",
});
states.Add(new State
{
CountryCode = "WF",
Pin = "5,143",
Offset = 12,
Points = "-3,150,10,138",
});
Trace.WriteLine($"DB sync: start states");
Trace.WriteLine("Create DB if not exists");
System.Data.Entity.Database.SetInitializer(new DBInitializer());
using (var nameContext = new DBContext())
{
nameContext.BulkInsert(states);
nameContext.SaveChanges();
}
Trace.WriteLine($"DB sync: end states");
}