在LINQ to SQL或Entity Framework中更新操作 [英] Update Operation in LINQ to SQL or Entity Framework

查看:108
本文介绍了在LINQ to SQL或Entity Framework中更新操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新LINQ to SQL中的一列。我的查询如下: -

I want to update one column in LINQ to SQL. My Query is like :-

private void UpdateCourse()
{
      OperationDataContext OdContext = new OperationDataContext();
      //Get Single course which need to update
      COURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");
      //Field which will be update
      objCourse.course_desc = "Bachelor of Technology";
      // executes the appropriate commands to implement the changes to the database
      OdContext.SubmitChanges();
}



我的问题是首先从 COURSE 中获取符合标准的记录比它更新它。这是正确的方法吗?或者SP是正确的解决方案。



建议!


My question is first it take record from COURSEs which match the criteria Than it update it. It this right approach? or SP is right solution here.

Suggest!

推荐答案

1。如果需要更新单个值。转到LINQ并使用Entity Framework进行更新。



int num = OdContext.SaveChanges();



当我们执行OdContext.SaveChanges()时,它会将此修改保存到数据库中。此方法还返回数据库中更新的行数。



2.用于保存或更新列数。如果性能是一个主要问题,最好选择Store程序。
1. If you need to update a single value. Go for LINQ and update using Entity Framework.

int num = OdContext.SaveChanges();

It saves this modification to the database when we do OdContext.SaveChanges(). This method also returns the number of rows updated in the database.

2. For saving or updation of number of columns. Better to go for Store procedures if performance is a major concern.


它是正确的:-) taht方式你只更新course_desc字段......怎么回事?
It's Right :-) taht way you only update the course_desc field... What's the matter?


是的,两者都能正常工作。但哪一个快速有效?假设我在表中有15列,所有都是varchar(max),而不是LINQ TO SQL首先获取所有列,而更新意味着2匹配到DB。哪一个(上面提到的方式或存储过程)是快速和首选的?
Yes both will work fine. But which one is fast and efficient? Suppose i have 15 column in table all are varchar(max) than LINQ TO SQL first fetch all column and than update means 2 hit to DB. Which one(above mention way OR Stored procedure) is fast and preferred?


这篇关于在LINQ to SQL或Entity Framework中更新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆