[英]Update Object without Select EF6 MySQL
Is it possible to update objects with Entity Framework, without grabbing them first? 是否可以使用Entity Framework更新对象,而无需先获取它们?
Example: Here, I have a function that provides a Primary Key to locate the objects, pulls them, then updates them. 示例:在这里,我有一个提供主键来定位对象,拉出它们然后更新它们的函数。 I would like to eliminate having to pull the objects first, and simply run an UPDATE query. 我想消除不必先拉对象,而只需运行UPDATE查询。 Removing the need for the SELECT query being generated. 无需生成SELECT查询。
public async Task<int> UpdateChecks(long? acctId, string payorname, string checkaccountnumber, string checkroutingnumber, string checkaccounttype)
{
using (var max = new Max(_max.ConnectionString))
{
var payments = await
max.payments.Where(
w =>
w.maindatabaseid == acctId && (w.paymentstatus == "PENDING" || w.paymentstatus == "HOLD")).ToListAsync();
payments.AsParallel().ForAll(payment =>
{
payment.payorname = payorname;
payment.checkaccountnumber = checkaccountnumber;
payment.checkroutingnumber = checkroutingnumber;
payment.checkaccounttype = checkaccounttype;
payment.paymentmethod = "CHECK";
payment.paymentstatus = "HOLD";
});
await max.SaveChangesAsync();
return payments.Count;
}
}
You can use the Attach()
command to attach an entity you already know exists and then call SaveChanges()
will will call the appropriate update method. 您可以使用Attach()
命令附加一个已经存在的实体,然后调用SaveChanges()
将调用适当的更新方法。 Here is some sample code from the MSDN article on the topic: 这是有关该主题的MSDN文章中的一些示例代码:
on the subject: 就此主题而言:
var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" };
using (var context = new BloggingContext())
{
context.Entry(existingBlog).State = EntityState.Unchanged;
// Do some more work...
context.SaveChanges();
}
Note that this is general EF logic, not related to any specific database implementation. 请注意,这是通用的EF逻辑,与任何特定的数据库实现都不相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.