질문으로 파일 시스템에 이미지를 저장하고 데이터베이스에 링크를 저장하려고합니다. 하지만 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);
}
}
하지만 리포지토리가 항목을 저장하거나 업데이트 한 후에 데이터베이스를 보면 이미지가 null입니다. 이미지 이름과 같은 무언가를 할당하려고했으나 작동했지만 이미지 경로가 작동하지 않습니다 !! 왜 이런 일이 일어나는지 혼란 스럽습니다. 누구든지 어떤 생각을 가지고 있니?
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 코드가 추가되었습니다. 리포지토리는 모든 엔티티를 저장하거나 업데이트 할 수 있습니다. 또한 이미지를 db에 이진 파일로 저장할 수 있지만,이 상황을 위해 앱 디렉토리에 저장하는 것이 좋습니다. –