导航属性为null

时间:2016-11-01 19:41:56

标签: asp.net-mvc-5 include ef-code-first entity-framework-6 navigation-properties

当我尝试通过include()加载它时,SellingRequest中的相册导航属性为空,而看起来每件事情都没问题!

这些是代码:

public class Album
{
    public int Id { get; set; }
    public string Note { get; set; }
    public virtual SellingRequest SellingRequest { get; set; }
    public int? SellingRequestId { get; set; }
    public List<Photo> Photos { get; set; }
    public virtual MortgageAndRent MortgageAndRent { get; set; }
    public int? MortgageAndRentId { get; set; }
}
public class SellingRequest
{
    #region Properies

    public int Id { get; set; }
    public virtual  Album Album { get; set; }
    public  int AlbumId { get; set; }

    #endregion Properies
}

这是我为SellingRequest创建专辑的地方。

public ActionResult DoUpload(HttpPostedFileBase file, UploadPopupViewModel uploadPopupViewModel)
    {
        if (file != null && file.ContentLength > 0)
        {
            string path = Path.Combine(Server.MapPath("~/Contents/Images"), Path.GetFileName(file.FileName));
            file.SaveAs(path);

            Photo photo = new Photo() { Path = path };
            ResponseMessage<Album> album = new ResponseMessage<Album>();

            if(uploadPopupViewModel.SellingRequestId!=0)
                album = _albumService.GetAlbumBySellingRequestId(uploadPopupViewModel.SellingRequestId);

            if (uploadPopupViewModel.MortgageAndRentId != 0)
                album = _albumService.GetAlbumByMortgageAndRentId(uploadPopupViewModel.SellingRequestId);

            if (album.IsSuccess)
            {
                photo.AlbumId = album.Result.Id;
            }
            else
            {
                Album newAlbum = new Album();
                if (uploadPopupViewModel.SellingRequestId != 0)
                    newAlbum.SellingRequestId = uploadPopupViewModel.SellingRequestId;

                if (uploadPopupViewModel.MortgageAndRentId != 0)
                    newAlbum.MortgageAndRentId = uploadPopupViewModel.MortgageAndRentId;

                ResponseMessage<Album> beingSavedAlbum = _albumService.Insert(newAlbum);

                ResponseMessage<SellingRequest> sellingRequest = _sellingRequestService.GetById(uploadPopupViewModel.SellingRequestId);
                if(sellingRequest.IsSuccess)
                {
                    sellingRequest.Result.AlbumId = newAlbum.Id;
                    _sellingRequestService.Update(sellingRequest.Result);
                }
                if(beingSavedAlbum.IsSuccess)
                photo.AlbumId = beingSavedAlbum.Result.Id;
            }
            ResponseMessage<Photo> beingSavedPhoto = _photoService.Insert(photo);
            if (beingSavedPhoto.IsSuccess)
            {
                return RedirectToAction("UploadPopup", "Photo", uploadPopupViewModel);
            }
            else
            {
                ModelState.AddModelError("ImageError", beingSavedPhoto.ErrorMessages[0]);
                return View("AddPhoto");
            }

        }
        else
        {
           ModelState.AddModelError("ImageError", "Please choose a photo.");
            return View("AddPhoto");
        }
    }
}

以下是我尝试查询的地方:

public IEnumerable<TEntity> GET(Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = _context.Set<TEntity>();

        if (filter != null)
            query = query.Where(filter);

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            query = query.Include(includeProperty);

        if (orderBy != null)
            return orderBy(query).ToList();
        else
            return query.ToList();
    }

因此,我尝试通过include加载相册。

public List<SellingRequest> GetAllSellingRequests()
    {

        List<SellingRequest> sellingRequests = _sellingRepository.GET(null, includeProperties: "Address,Album.Photos", orderBy: sR => sR.OrderBy(s => s.RegisteredDate).OrderByDescending(s => s.RegisteredDate)).ToList();
        return sellingRequests;
    }

使用此函数,我有Address属性,但Album为null!反之亦然。我的意思是当我从专辑中包含sellingRequest时,它也返回null!这是我可以包括所有其他实体没有问题!

修改 这是我的数据库中表的模式:
SellingRequest Table
And this is the picture of Get.

任何建议都会提前得到赞赏。 问候

0 个答案:

没有答案