最有效的方法使用LINQ更新到SQL [英] Most efficient way to update with LINQ to SQL
问题描述
我可以升级我的员工记录如下函数给出还是我必须让员工集合的查询,然后再更新数据?
公众诠释updateEmployee(App3_EMPLOYEE员工)
{
DBContextDataContext DB =新DBContextDataContext();
db.App3_EMPLOYEEs.Attach(员工);
db.SubmitChanges();
返回employee.PKEY;
}
还是我必须做到以下几点?
公众诠释updateEmployee(App3_EMPLOYEE员工)
{
DBContextDataContext DB =新DBContextDataContext();
App3_EMPLOYEE EMP = db.App3_EMPLOYEEs.Single(E => e.PKEY == employee.PKEY);
db.App3_EMPLOYEEs.Attach(员工,EMP);
db.SubmitChanges();
返回employee.PKEY;
}
但我不希望使用第二个选项。是否有更新数据的任何有效的方式?
我通过两种方式得到这个错误:
的尝试已经取得了附加或添加这不是新的实体,可能已经从另一DataContext的加载。这是不支持的。
块引用>解决方案我发现下面的解决了这个问题:
1)获取和更新实体(我要去,因为它的确定对我来说)用这种方式
公众诠释updateEmployee(App3_EMPLOYEE员工)
{
AppEmployeeDataContext DB =新AppEmployeeDataContext();
App3_EMPLOYEE EMP = db.App3_EMPLOYEEs.Single(E => e.PKEY == employee.PKEY);
emp.FIRSTNAME = employee.FIRSTNAME; //一个拷贝酒店只有一
db.SubmitChanges();
返回employee.PKEY;
}2)禁止ObjectTrackingEnabled如下
//但在这种情况下,延迟加载不支持
公共AppEmployeeDataContext():
基地(全球:: LinqLibrary.Properties.Settings.Default.AppConnect3DBConnectionString,mappingSource)
{
this.ObjectTrackingEnabled = FALSE;
OnCreated();
}3)拆下所有相关的对象
部分类App3_EMPLOYEE
{
公共无效分离()
{
this._APP3_EMPLOYEE_EXTs =默认(的EntityRef< APP3_EMPLOYEE_EXT>);
}
} 公众诠释updateEmployee(App3_EMPLOYEE员工)
{
AppEmployeeDataContext DB =新AppEmployeeDataContext();
employee.Detach();
db.App3_EMPLOYEEs.Attach(员工,真正的);
db.SubmitChanges();
返回employee.PKEY;
}4)使用时间戳列
http://www.west-wind.com/weblog/posts/135659.aspx
5)用于更新数据来创建存储过程,并通过DB方面称之为
Can I update my employee record as given in the function below or do I have to make a query of the employee collection first and then update the data?
public int updateEmployee(App3_EMPLOYEE employee) { DBContextDataContext db = new DBContextDataContext(); db.App3_EMPLOYEEs.Attach(employee); db.SubmitChanges(); return employee.PKEY; }
Or do I have to do the following?
public int updateEmployee(App3_EMPLOYEE employee) { DBContextDataContext db = new DBContextDataContext(); App3_EMPLOYEE emp = db.App3_EMPLOYEEs.Single(e => e.PKEY == employee.PKEY); db.App3_EMPLOYEEs.Attach(employee,emp); db.SubmitChanges(); return employee.PKEY; }
But I don't want to use the second option. Is there any efficient way to update data?
I am getting this error by using both ways:
An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.
解决方案I find following work around to this problem :
1) fetch and update entity (I am going to use this way because it's ok for me )
public int updateEmployee(App3_EMPLOYEE employee) { AppEmployeeDataContext db = new AppEmployeeDataContext(); App3_EMPLOYEE emp = db.App3_EMPLOYEEs.Single(e => e.PKEY == employee.PKEY); emp.FIRSTNAME = employee.FIRSTNAME;//copy property one by one db.SubmitChanges(); return employee.PKEY; }
2) disable ObjectTrackingEnabled as following
// but in this case lazy loading is not supported public AppEmployeeDataContext() : base(global::LinqLibrary.Properties.Settings.Default.AppConnect3DBConnectionString, mappingSource) { this.ObjectTrackingEnabled = false; OnCreated(); }
3) Detach all the related objects
partial class App3_EMPLOYEE { public void Detach() { this._APP3_EMPLOYEE_EXTs = default(EntityRef<APP3_EMPLOYEE_EXT>); } } public int updateEmployee(App3_EMPLOYEE employee) { AppEmployeeDataContext db = new AppEmployeeDataContext(); employee.Detach(); db.App3_EMPLOYEEs.Attach(employee,true); db.SubmitChanges(); return employee.PKEY; }
4) use Time stamp in the column
http://www.west-wind.com/weblog/posts/135659.aspx
5) Create stored procedure for updating your data and call it by db context
这篇关于最有效的方法使用LINQ更新到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!