【发布时间】:2012-03-07 07:22:31
【问题描述】:
我正在开发一个 HRM 应用程序来从数据库导入和导出 xml 数据。应用程序接收员工条目的导出 xml 数据。我使用 linq to xml 导入了 xml 文件,并在其中将 xml 转换为相应的对象。然后我想附加(更新)员工对象。
我尝试使用
//linqoper class for importing xml data and converts into IEnumerable employees object.
var emp = linqoper.importxml(filename.xml);
Using (EmployeedataContext db = new EmployeedatContext){
db.attachAllonSubmit(emp);
db.submitchange();
}
但我得到了错误
“An entity can only be attached as modified without original state if it declares as version member or doesn't have an update check policy”.
我还可以选择检索每个员工,并使用这种格式从 xml 数据中为新员工赋值。
//import IEnumerable of Employee objects
var employees = = linqoper.importxml(filename.xml)
using(Employeedatacontext db = new Employeedatacontext){
foreach(var empobj in employees)
{
Employee emp = db.Employee.where(m=>m.id==empobj.Id);
emp.FirstName=empobj.FirstName;
emp.BirthDate=empobj.BirthDate;
//….continue
}
db.submitChanges();
}
但是上面的问题是我必须遍历整个员工对象,这很烦人。 那么有没有其他方法,我可以使用 LINQ to SQL 附加(更新)数据库中的员工实体。
我在 SO 上看到了一些类似的链接,但似乎都没有帮助。
https://stackoverflow.com/questions/898267/linq-to-sql-attach-refresh-entity-object
【问题讨论】:
标签: c# .net linq linq-to-sql