开发者

Entity Framework trouble - ASP.NET 4 MVC 3 - SportsStore Project

(Apress PRO: ASP.NET MVC 3 Framework - by Adam Freeman and Steven Sanderson - third edition)

(As a heads-up: I am new to ASP.NET and building multi-project solutions in Visual Studio.)

I've been following along very well in the book up to page 298. (I am using SQL Express.) I've hit a snag when I try to compile and run my solution in Visual Studio 2010 (SP1).

The issue occurs when I add the ADO.NET Entity Data Model (.edmx file) to my SportsStore.Domain project under Concrete/ORM/ folder (which is odd... the book on page 291 makes it sound like that file should have already existed...)

... and I update/refresh the model in the Entity designer to include the two new properties (ImageData and ImageMimeType) --> the compiler throws an exception under:

using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Controllers {
    public class ProductController : Controller {

        public ViewResult List(string category, int page = 1) {
            ProductsListViewModel viewModel = new ProductsListViewModel {
                Products = repository.Products
                    .Where(p => category == null || p.Category == category)
                    .OrderBy(p => p.ProductID)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo { 
                     CurrentPage = page,
                     ItemsPerPage = PageSize,
                     TotalItems = category == null ?
                         repository.Products.Count() :
                         repository.Products.Where(e => e.Category == category).Count()
                },
                CurrentCategory = category
            };

            return View(viewModel);
        }
    }
}

saying "Could not find the conceptual model type for SportsStore.Domain.Concrete.ORM.Product."

When I remove the .edmx file, the solution compiles successfully --> but of course, I cannot use the new image properties to add pictures to the database. Page 291 states why: no connection between new columns in Product Table and Product class. (I can find and select an image (on disk) on the webpage and hit submit, but nothing is added to the database afterwards.)

I'm not sure where to start really. If it would help to show more of my project I definitely will. (it's pretty开发者_运维问答 much identical to the book's.)

(There actually was another snag with the Product class under SportsStore.Domain.Entities. The "ActionResult Edit(...)" method from AdminController under SportsStore.WebUI.Controllers did not like Product's ImageData property being declared as byte and not byte[]. I fixed that of course. Should I report that in the book's errata?)


I went through the same problem and here is how I fixed it.

  1. Do not add the edmx file
  2. Update the SaveProduct method in EFProductRepository.cs

Something like this:

public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
            Product prod = context.Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();
            prod.Category = product.Category;
            prod.Description = product.Description;
            prod.ImageData = product.ImageData;
            prod.ImageMimeType = product.ImageMimeType;
            prod.Name = product.Name;
            prod.Price = product.Price;
        }
        context.SaveChanges();
    }

I think this is not the best way to do it, because we do the mapping by hand.

EDIT: better way for doing it

public void SaveProduct(Product product)
    {
        if (product.ProductID == 0)
        {
            context.Products.Add(product);
        }
        else
        {
            context.Entry(product).State = EntityState.Modified;
        }
        context.SaveChanges();
    }


I was having the same exact issue and got around it by getting rid of .edmx file entirely. Images seem to be saving and displaying fine without it. Just make sure your db, controller, and view are consistent with what is in the book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜