View Full Version : سرعت پائین در دستور SaveChanges در EF
spicirmkh
سه شنبه 12 اردیبهشت 1391, 12:25 عصر
سلام
من بعد از Update کردن دیتا بیس وقتی دستور SaveChanges می زنم خیلی زمان طول می کشد . تعداد رکورد که می خواهم به روز کنم حدود 70 هزار رکورد است و روی رکورد مورد نظر عمل ایندکس انجام داده ام
لازم به ذکر است و DbContext بصورت private تعریف کرده ام
در حالی که این عمل توی SQl حدود یک ثانیه طول می کشد
int Pubid = int.Parse(cbxFrom.SelectedValue);
int NewPubid = int.Parse(cbxTo.SelectedValue);
var Abstract = DbContext.tblEjournalA
.Where(w => w.PubID == Pubid).ToList() ;
Abstract.ForEach(u => u.PubID = NewPubid);
var publisher =DbContext.tblEjournalP
.Where(w => w.PubID == Pubid).ToList();
publisher.ForEach(DbContext.DeleteObject);
DbContext.SaveChanges();
uniqueboy_ara
چهارشنبه 13 اردیبهشت 1391, 23:09 عصر
من به شخصه هیچوقت از این روش استفاده نمی کنم ( تعریف Contex به صورت سراسری )
همیشه Contex رو داخل تگ Using استفاده می کنم و هیچوقت هم مشکلی پیش نمیاد!!!
شاید مشکل پروژه شما واسه همین باشه
spicirmkh
پنج شنبه 14 اردیبهشت 1391, 11:23 صبح
من به شخصه هیچوقت از این روش استفاده نمی کنم ( تعریف Contex به صورت سراسری )
همیشه Contex رو داخل تگ Using استفاده می کنم و هیچوقت هم مشکلی پیش نمیاد!!!
شاید مشکل پروژه شما واسه همین باشه
من هم بصورت سراسری تعریف نکردم به اینصورت
private EJournalSpicEntities _dbContext;
public EJournalSpicEntities DbContext
{
get
{
if (_dbContext == null) _dbContext = new EJournalSpicEntities();
return _dbContext;
}
}
amir3321
یک شنبه 17 اردیبهشت 1391, 10:48 صبح
فکر کنم دلیل این باشد که entityframework برای هر با حذف یک بار query رو اجرا می کنه ولی می تونید از روش سمت سرور استفاده کنید مانند زیر
context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE CustomerID = {0}", customerId);
spicirmkh
دوشنبه 18 اردیبهشت 1391, 09:18 صبح
فکر کنم دلیل این باشد که entityframework برای هر با حذف یک بار query رو اجرا می کنه ولی می تونید از روش سمت سرور استفاده کنید مانند زیر
context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE CustomerID = {0}", customerId);
تفاوت 2 دستور ExecuteStoreCommand و ExecuteStoreQuery چیست ؟
amir3321
دوشنبه 18 اردیبهشت 1391, 11:28 صبح
فکر کنم فرق اصلی در بازگشتی و بدون بازگشت بودن اونهاست یعنی در query شما باید نوع برگشتی رو مشخص کنید ولی در command خیر
context.ExecuteStoreCommand
This command can be used to execute SQL/PSQL commands that don't return any values for e.g. insert/delete statements.
1: using (var context = new EFRecipesEntities()) 2: { 3: context.ExecuteStoreCommand("delete from chapter10.Product"); 4: context.ExecuteStoreCommand("insert into chapter10.Product values ('Chai', 'Beverage')"); 5: }
context.ExecuteStoreQuery<TElement>
If you need to execute a SQL/PSQL that returns record sets then use context.ExecuteStoreQuery<TElement> where TElement can be an existing Entity in the CSDL of the EDMX file, or if the result does not map to an existing Entity in the CSDL then use context.ExecuteStoreQuery<DbDataRecord> to iterate over the columns of the returned results manually using indexers on DbDataRecord to access column values. Note that context.ExecuteStoreQuery<> returns a ObjectResult<TElement> which be iterated over only once as opposed to ObjectSet<T> or ObjectQuery<T> which can be iterated over multiple times.
1: using (var context = new EFRecipesEntities()) 2: { 3: string sql = "select * from Chapter3.Student where Degree = @Major"; 4: var args = new DbParameter[] { new SqlParameter { ParameterName = "Major", Value = "Masters" } }; 5: var students = context.ExecuteStoreQuery<Student>(sql, args); 6: }
rezahajali
جمعه 22 اردیبهشت 1391, 11:55 صبح
سلام not only sqlچیست و چه خصوصیاتی داره و در کجا استفاده میشه ؟NO SQL
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.