正如问题所暗示的,我想将图像存储到文件系统中并将指向它的链接保存在数据库中。但 NHibernate 不会将文件路径保存在数据库中。这是代码:
[HttpPost]
public ActionResult Edit(Item item, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
if (image != null)
{
string imageName = image.FileName;
string location = Path.Combine(Server.MapPath("~/Content/Images/ItemImages/") , imageName);
image.SaveAs(location);
item.Image= imageName;
}
menuItemRepository.SaveOrUpdate(item);
// here the debug show the image path has correctly assigned to the image property
Debug.WriteLine(item.Image);
TempData["message"] = string.Format("{0} has been saved", item.Name);
return RedirectToAction("Index", item.Parent);
}
else
{
// there is something wrong with the data values
return View(Item);
}
}
但是在存储库保存或更新项目后,当我查看数据库时,图像为空。我试图分配像图像名称这样的元素,它确实有效,但图像路径不起作用!我很困惑为什么会这样。有人有什么主意吗?
public class Item
{
public virtual string Image { get; set; }
}
public calss ItemMap : ClassMap<Item>
{
public ItemMap()
{
Map(x => x.Image).Length(100);
}
}
//////////存储库
public T SaveOrUpdate(T entity)
{
session.SaveOrUpdate(entity);
return entity;
}