我有一个小型的演示应用程序来解决存储在数据库中的二进制数据。请参阅下面的相关代码:
实体类:
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Display(Name = "Full Name:")]
public string Name { get; set; }
[DataType(DataType.Upload)]
[Display(Name = "Photo:")]
public byte[] ImageData { get; set; }
[HiddenInput(DisplayValue = false)]
public string ImageMimeType { get; set; }
修改操作:
[HttpGet]
public ActionResult Edit(int id)
{
var mensPlayer = _dataSource.MensPlayers.FirstOrDefault(p => p.Id == id);
return View(mensPlayer);
}
[HttpPost]
public ActionResult Edit(MensPlayer mensPlayer, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
//Added line below
_dataSource.ImageTemp(mensPlayerInDb, mensPlayer);
if (image != null)
{
mensPlayer.ImageMimeType = image.ContentType;
mensPlayer.ImageData = new byte[image.ContentLength];
image.InputStream.Read(mensPlayer.ImageData, 0, image.ContentLength);
}
//Added line below
mensPlayer.ImageData = mensPlayerInDb.ImageData;
//Save Player
_dataSource.Update(mensPlayer);
_dataSource.Save();
TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
}
return View(mensPlayer);
}
GetImage方法:
public FileContentResult GetImage(int id)
{
var image = _dataSource.MensPlayers.FirstOrDefault(p => p.Id == id);
if (image != null)
{
return File(image.ImageData, image.ImageMimeType);
}
return null;
}
显示图片:
@if (Model.ImageData != null) {
<div >
<img width="300" height="400" src="@Url.Action("GetImage", "MensPlayer",
new { Model.Id })" alt="Player Image"/>
</div>
}
DataSource类
public interface IDataSource
{
IQueryable<MensTeam> MensTeams { get; }
IQueryable<MensPlayer> MensPlayers { get; }
IQueryable<MensHome> MensHomes { get; }
void Save();
void Update(MensPlayer mensPlayer);
void Delete();
void ImageTemp(MensPlayer mensPlayerInDb, MensPlayer mensPlayer);//I added this
}
在Db课程中
void IDataSource.Save()
{
SaveChanges();
}
void IDataSource.Update(MensPlayer mensPlayer)
{
Entry(mensPlayer).State = EntityState.Modified;
}
//Added code below
void IDataSource.ImageTemp(MensPlayer mensPlayerInDb, MensPlayer mensPlayer)
{
Entry(mensPlayerInDb).CurrentValues.SetValues(mensPlayer);
}
我的问题是,每当我尝试编辑播放器时,每个数据都可以从数据库中很好地检索,但是当我在编辑后点击保存时,ImageData会丢失,更像是用空值替换原始数据。就像,应用程序希望我在每次尝试编辑时重新上传图像。
我该怎么做才能清理它?
答案 0 :(得分:0)
好吧,如果image
为null
,则您实体mensPlayer.ImageData
中的null
为_dataSource.Update(mensPlayer)
。稍后当您致电Modified
时,您将实体的状态设置为Update
(至少我猜Modified
只是将状态设置为null
),因此您告诉EF您也修改了图像(即将其设置为var mensPlayerInDb = context.MensPlayers.Find(mensPlayer.Id);
mensPlayer.ImageData = mensPlayerInDb.ImageData; // save the ImageData
// copy changed values to loaded entity and save it, ImageData is unchanged
context.Entry(mensPlayerInDb).CurrentValues.SetValues(mensPlayer);
context.SaveChanges();
),EF将保存该更改。
为了解决这个问题,你可以先加载包含数据库中图像的实体......
context.Entry(mensPlayer).State = EntityState.Modified;
context.Entry(mensPlayer).Property(m => m.ImageData).IsModified = false;
...或者您可以尝试:
Modified
后一种方法不适用于EF&lt; 5.0(因为一旦实体被标记为false
,禁止将属性的Modified
状态设置为_dataSource
,但它应该与EF 5一起使用。
您需要将其中一个解决方案集成到Update
服务类中,可能会引入新方法,因为它不适用于您的常规{{1}}方法。