我查遍了互联网,仍然找不到解决方案。
我尝试了附加方法:
public static void updatePhoto(string name, string albumName, string newName, string newPath)
{
//updates photo... no delete and adding...
var photo = new Image(){Label=newName, Path = newPath};
using (var db = new EzPrintsEntities())
{
db.Images.Attach(photo);
db.SaveChanges();
}
}
但这根本没有做任何事情。
那么问题来了,下面的代码中如何通过EF实现对sql数据库的UPDATE呢?
public static void updatePhoto(string name, string albumName, string newName, string newPath)
{
EzPrintsEntities db = new EzPrintsEntities();
}
最佳答案
如果您要更新现有照片,则需要加载它并更改现有值:
public static void updatePhoto(string name, string albumName, string newName, string newPath)
{
using (var db = new EzPrintsEntities())
{
// Load photo
var photo = db.Images.FirstOrDefault(i => i.Label == name && i.Album == albumName);
if (photo == null)
{
// no matching photo - do something
}
// Update data
photo.Label = newName;
photo.Path = newPath;
db.SaveChanges();
}
}
关于c# - 与 SQL UPDATE 等效的 Entity Framework 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12465213/