作爲問題意味着我想要將圖像存儲到文件系統並在數據庫中保存鏈接。 但NHibernate不保存數據庫中的文件路徑。這裏是代碼:在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);
}
}
但復位器後保存或更新的項目,當我看着數據庫,圖像爲空。我試圖指定像圖像名稱ele,它確實工作,但圖像路徑不工作!我很困惑爲什麼會發生這種情況。有人有什麼主意嗎?
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;
}
menuItemRepository.SaveOrUpdate的代碼是什麼? – MichaC
@MichaC code added。該存儲庫可以保存或更新我的所有實體。我也可以將圖像保存爲二進制文件到數據庫,但我認爲將其保存到應用程序目錄很適合這種情況。 –