BulkInsert در EF CodeFirst
نویسنده: م کریمی
تاریخ: ۱۳۹۲/۱۲/۲۸ ۱۱:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Personel
{
[Key]
public int PersonelID { get; set; }
[MaxLength(15)]
public string Name { get; set; }
[MaxLength(25)]
public string Family { get; set; }
[MaxLength(10)]
public string CodeMelli { get; set; }
} public class PersonalContext : DbContext
{
public DbSet<Personel> Personel { get; set; }
public override int SaveChanges()
{
return base.SaveChanges();
}
} Install-Package EntityFramework.BulkInsert-ef6
public ActionResult Insert()
{
int Counter = 1000;
List<Personel> Lst = new List<Personel>();
// شبیه سازی خواندن رکوردها از فایل اکسل
for (int i = 0; i < Counter; i++)
{
Lst.Add(new Personel
{
CodeMelli = "0000000000",
Family = "Karimi",
Name = "Mohammad"
});
}
PersonalContext db = new PersonalContext();
db.BulkInsert(Lst);
db.SaveChanges();
return View();
}
var personnels = dataSet.Tables["Personnel"].AsEnumerable(); db.BulkInsert(personnels.ToList());
db.Personel.BulkInsert(Lst);
using(NewsEntities context = new NewsEntities())
{
try
{
List<Category> catlist = new List<Category>();
for (int i = 0; i < 1000; i++)
{
Category cat = new Category { CategoryName = "cat" + i.ToString() };
catlist.Add(cat);
}
context.BulkInsert(catlist);
context.SaveChanges();
MessageBox.Show("Added");
}
catch(Exception ex)
{
MessageBox.Show(Error.ShowError(ex));
}
} db.BulkInsert(entities);
Field_ConnectionString was not found in Type System.Data.SqlConnection Parametername:PropName
protected virtual string ConnectionString
{
get
{
return (string)DbConnection.GetPrivateFieldValue("_connectionString");
}
} protected virtual string ConnectionString {
get {
return (string)DbConnection.ConnectionString;
}
} EntityFramework.BulkInsert-ef6-ext
var bulk = new BulkOperation(connection); bulk.BulkInsert(dt);
person prs = new Person() {name = "ali"};
db.BulkInsert(prs);