Lately we’ve been having issues on entity framework saving data objects even though we don’t ask it to. Here’s an example:
public virtual ActionResult Index()
{
var productListViewModel = new ProductListViewModel
{
Products = ProductService.GetAllProducts()
};
// temporarily override file url to display images
foreach (var product in productListViewModel.Products)
{
product.ImageFileName = FileUploadService.GetProductThumbnailUrl(product);
}
return View(productListViewModel);
}
The GetProductThumbnailUrl() function returns us with a formated path string made out of a static URL + ImageFileName. However for obvious reasons we don’t want to save this path and just use it temporarily for the view.
But here’s the strange part: sometimes all of these products get saved to the database which gives us malformed urls that we can’t use.
So my question right now is what could cause this? (I’m using unity for dep. injection)
And as for a fix I just don’t change anything in the data object, and instead use a rendered action:
public ActionResult GetProductUrl(DomainModel.Product p)
{
return Content(FileUploadService.GetProductThumbnailUrl(p));
}
And in the view:
@Html.Action(MVC.Admin.Product.GetProductUrl(p))
Somewhere in there, you’re calling commit changes against the context you’re pulling from. That is the only way that is getting saved back.