2015-07-18 19 views
5

Ngày tốt, tôi mới ở ASP.NET và tôi không thể hiểu tại sao mã của tôi không hoạt động. Tôi có mô hình:ASP.NET lưu mô hình thực thể phức tạp

using System.ComponentModel.DataAnnotations; 
using System.Drawing; 
using System.Globalization; 

namespace WebApp.Models 
{ 
public class News 
{ 
    public int NewsId { get; set; } 
    public string title { get; set; } 
    public string description { get; set; } 
    public virtual Picture picture { get; set; } 
    public int guid { get; set; } 
} 

public class Picture 
{ 
    public int PictureId { get; set; } 
    public byte[] image { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
    public int hash { get; set; } 
} 
} 

Và tôi đang cố gắng để tạo mới "Tin tức" qua đường bưu điện có dạng:

// POST: News/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(News news, HttpPostedFileBase uploadImage) 
    { 
     if (ModelState.IsValid && uploadImage != null) 
     { 
      byte[] imageData = null; 
      using (var binaryReader = new BinaryReader(uploadImage.InputStream)) 
      { 
       imageData = binaryReader.ReadBytes(uploadImage.ContentLength); 
      } 
      news.picture = new Picture() 
      { 
       hash = 0, 
       image = imageData, 
       width = 0, 
       height = 0 
      }; 
      db.News.Add(news); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(news); 
    } 

Nhưng khi tôi lấy dữ liệu từ db Tôi nhận được null pointer ngoại lệ: , khi tôi gọi trình gỡ lỗi, tôi tìm ra rằng giá trị "news.picture" là null. Nhưng trước khi db.SaveChanges() nó 100% không null. Có vẻ như tôi đang làm điều gì đó ngu ngốc sai, bcs tôi không thể tìm thấy some1 người phải đối mặt với vấn đề này. Cám ơn.

+0

Tôi không biết tại sao, nhưng khi tôi thêm tài sản ảo tất cả các công trình : "Hình ảnh ảo công khai {get; set;}" –

+0

Vâng thuộc tính ảo cho biết rằng điều này sẽ được điều hướng lazily. Nói chung mọi người đều có 'public virtual' trên tất cả các thuộc tính điều hướng –

+0

Một số thay đổi tôi sẽ thực hiện - trước hết đặt một' public int PictureId {get; thiết lập;} 'trên mô hình tin tức, mà sẽ đặt một khóa nước ngoài ở đó. Sau đó, nó sẽ thuộc tính '[ForeignKey (" Picture ")]' trong đó '" Picture "' là thuộc tính điều hướng ảo. Trong thực thể hình ảnh, bạn có thể xóa 'Hình ảnh' khỏi tên' Id' như với thực thể tin tức. Điều này làm cho nó sạch hơn –

Trả lời

0

Tôi không biết tại sao, nhưng khi tôi thêm tài sản ảo tất cả các công trình:

public virtual Picture picture { get; set; } 
0

hãy thử như sau

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(News news, HttpPostedFileBase uploadImage) 
    { 
     if (ModelState.IsValid && uploadImage != null) 
     { 
      byte[] imageData = null; 
      using (var binaryReader = new BinaryReader(uploadImage.InputStream)) 
      { 
       imageData = binaryReader.ReadBytes(uploadImage.ContentLength); 
      } 
      var picture = new Picture() 
      { 
       hash = 0, 
       image = imageData, 
       width = 0, 
       height = 0 
      }; 
      db.Pictures.Add(picture); // make sure that the picture was tracked by the EF 
      news.picture = picture; 
      db.News.Add(news); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(news); 
    } 
+1

Điều đó sẽ không thành vấn đề, nếu bạn gắn mô hình thông qua một thuộc tính điều hướng, nó sẽ tạo ra thực thể trong cơ sở dữ liệu và liên kết nó với thực thể tin tức. –

0

tôi sẽ làm cho những thay đổi sau và xem nếu nó giúp.

namespace WebApp.Models 
{ 
    public class News 
    { 
     public int Id { get; set; } 
     public string Title { get; set; } 
     public string Description { get; set; } 

     [ForeignKey("Picture")] 
     public int PictureId { get; set; } 
     public virtual Picture Picture { get; set; } 

     // not sure why this is here 
     public int guid { get; set; } 
    } 

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

     public byte[] ImageData { get; set; } 
     public string ContentType { get; set; } 

     public int width { get; set; } 
     public int height { get; set; } 
     public int hash { get; set; } 
    } 
} 

Controller:

// POST: News/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(NewsDto news, HttpPostedFileBase uploadImage) 
{ 
    if (ModelState.IsValid && uploadImage != null) 
    { 
     var imageData = byte[uploadImage.ContentLength]; 
     using (var binaryReader = new BinaryReader(uploadImage.InputStream)) 
     { 
      imageData = binaryReader.ReadBytes(uploadImage.ContentLength); 
     } 

     var newsEntity = new New { 
      Title = news.Title, 
      Description = news.Description, 
      Picture = new Picture() 
      { 
       ImageData = imageData, 
       ContentType = contentType // I would get that from the HttpPostedFileBase 
      } 
     } 

     // I always wrap my database connections in a using. 
     using (var db = new DbContext()) { 
      db.News.Add(newsEntity); 
      db.SaveChanges(); 
     } 

     return RedirectToAction("Index"); 
    } 
    return View(news); 
} 
Các vấn đề liên quan