0

我正在尝试保存Photo具有byte[] File字段的班级。尝试使用上下文保存它时会引发错误

你调用的对象是空的。

但是在调试时我可以看到它不为空。我可以看到该类的所有属性,包括字节数组中的值。

 public class PhotoRepository 
    {
        private static BlogContext _ctx;

        public PhotoRepository()
        {
            _ctx = new BlogContext();
        }

        public static void Save(Photo p)
        {
            _ctx.Photos.Add(p);
            _ctx.SaveChanges();
        }
    }

控制器

 public class PhotoController : Controller
    {
        public ActionResult Index()
        {
            using (var ctx = new BlogContext())
            {
                return View(ctx.Photos.AsEnumerable());
            }
        }

        public ActionResult Upload()
        {
            return View(new Photo());
        }

        [HttpPost]
        public ActionResult Upload(PhotoViewModel model)
        {
            var photo =  new Photo();//Mapper.Map<PhotoViewModel, Photo>(model);
            if (ModelState.IsValid)
            {

                photo.AlternateText = model.AlternateText;
                photo.Description = model.Description;
                photo.File = MapStreamToFile(model.File);
                photo.Name = model.Name;
                PhotoRepository.Save(photo);
                return RedirectToAction("Index");
            }
            return View(photo);
        }

        public byte[] MapStreamToFile(HttpPostedFileBase file)
        {
            using (var stream = file.InputStream)
            {
                var memoryStream = stream as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                }
                return memoryStream.ToArray();
            }
        }
    }

照片

 public class Photo
    {
        public int Id { get; set; }

        public Byte[] File { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string AlternateText { get; set; }
    }

照片视图模型

 public class PhotoViewModel
    {
        public int Id { get; set; }

        public HttpPostedFileBase File { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string AlternateText { get; set; }
    }

在此处输入图像描述

4

1 回答 1

1

我认为问题在于它_ctx是空的。请注意,您声明它 and Save static,但它仅在构造函数中_ctx实例化。public PhotoRepository()只要它真的应该是静态的,就静态实例化它而不是在构造函数中:

 public static class PhotoRepository 
    {
        private static BlogContext _ctx = new BlogContext();

        public static void Save(Photo p)
        {
            _ctx.Photos.Add(p);
            _ctx.SaveChanges();
        }
    }

我还将类更改为static,因为我只看到它包含静态成员。如果您打算为这门课做更多,这可能是不正确的。

编辑:(感谢@pst)我从您的代码中看到更多,我认为这可能真的是一个更好的设计:

 public class PhotoRepository : IDisposable
    {
        private BlogContext _ctx = new BlogContext();

        public void Save(Photo p)
        {
            _ctx.Photos.Add(p);
            _ctx.SaveChanges();
        }

        void IDisposable.Dispose() { _ctx.Dispose(); }
    }

然后一定要PhotoRepository在完成后处理它。我在这里建议更改的原因BlogContext是一次性的,并且using在其他地方使用。

于 2012-06-17T17:57:13.177 回答