我試圖使用物體框架檢索資料表單資料庫,我有表“帖子”,它有很多屬性,其中一個屬性是“影像”,之前不需要影像屬性,所以我在“帖子”表中有很多存盤記錄“影像”的值為空,現在我將“影像”更改為必需,現在當嘗試從“帖子”表 asp.net 檢索舊記錄時啟動此例外(“資料為空。無法在空上呼叫此方法或屬性值”)。
我需要什么:我知道我可以通過從“影像”屬性中洗掉洗掉 [必需] 注釋來解決此問題,但我想保留此資料注釋并檢索所有記錄,因為“影像”值為 Null 或非 Null。
有什么辦法嗎?
我的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace ARID.Models
{
public class Post
{
[Key]
public Guid Id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "??? ??????? ??? ?? ?????? ??? 25 ??? ? 100 ???", MinimumLength = 25)]
[Display(Name ="???????")]
public string Title { get; set; }
[Required]
[StringLength(5000, ErrorMessage = "??? ??? ?????? ??????? ?? 900 ???? ?????? ?? 50 ????", MinimumLength = 50)]
[Display(Name = "???????")]
public string Body { get; set; }
[Display(Name = "????? ?????")]
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime DateTime { get; set; }
[Display(Name = "?????? ??????????")]
public bool IsCommentsAllowed { get; set; }
[StringLength(100)]
[Display(Name = "??????")]
[Required]
public string Image { get; set; }
[StringLength(100)]
[Display(Name = "????? ??????")]
public string File { get; set; }
[Display(Name = "??? ????????")]
public bool IsApproved { get; set; }
[Display(Name = "????? ???????")]
public bool IsHidden { get; set; }
[Display(Name = "????")]
public bool IsFeatured { get; set; }
[Display(Name = "???? ????? ?????")]
public bool IsGifted { get; set; }
[Display(Name = "??? ??????")]
public GiftType GiftType { get; set; }
[Display(Name = "??? ??????")]
public int Reads { get; set; }
[Display(Name = "??? ???????")]
public bool IsDeleted { get; set; }
[Display(Name = "???????")]
public int CommunityId { get; set; }
[Display(Name = "???????")]
public Community Community { get; set; }
[Display(Name = "??????")]
[StringLength(450)]
public string ApplicationUserId { get; set; }
[Display(Name = "??????")]
public ApplicationUser ApplicationUser { get; set; }
[StringLength(100, ErrorMessage = "??????? ????????? ?????", MinimumLength = 5)]
//[Required(ErrorMessage = "??? ??????? ????????? ?????")]
[Display(Name = "????? ???????")]
public string Tags { get; set; }
[Display(Name = "??? ???????")]
public GroupPostType PostType { get; set; }
[Display(Name = "??? ????? ?? ????? ???? ????")]
public bool IsPublishRequest { get; set; }
[Display(Name = "???? ??? ?????")]
public bool PublishRequestStatus { get; set; }
}
}
在控制器中:
public async Task<JsonResult> GetWallContents()
{
ARIDWallViewModel WallVM = new ARIDWallViewModel()
{
Posts = _context.Posts.Include(a => a.Community).Include(a => a.ApplicationUser).Where(a => a.Community.CommunityType == CommunityType.Personal && !a.IsHidden).OrderByDescending(a => a.DateTime).Take(25),
PostComments = _context.PostComments.Include(a => a.ApplicationUser).Include(a => a.Post).OrderByDescending(a => a.DateTime).Take(25),
Publications = _context.Publications.Include(a => a.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),
Courses = _context.Courses.Include(a=>a.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),
FreelancerReadyServices = _context.FreelancerReadyServices.Include(c => c.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),
Books = _context.Book.Include(c => c.ApplicationUser).Where(c => c.IsARIDPublications == true && c.IsFeatured == true).OrderByDescending(a => a.RecordDate).Take(25),
};
//ViewData["Count"] = _context.PostMetric.Count(a => a.PostId == id);
return Json(WallVM);
}
uj5u.com熱心網友回復:
簡短的回答是:不,你不能。
在您的資料庫中,已經有條目沒有設定影像屬性,現在您已將其更改為必需,因此根據新規則,您現有的條目不再有效。因此 EF 無法檢索它們,因為它只遵循您為模型設定的規則 - 此外,將屬性標記為必需(且不可為空)然后在現有條目仍然為 null 的情況下將是不好的編碼風格(您甚至不會收到這些空參考的編譯器警告,因為您的字串被標記為不可空)。
因此,您必須通過遷移修復現有條目(您可能在添加所需屬性后還沒有創建一個,因為否則 EF 會生成一個遷移,將資料庫中的列更改為“不可為空”,這會因為存在而失敗包含 null 的行 - 這就是為什么即使在對模型進行微小更改后也應該始終生成遷移的原因)。只需創建一個遷移并將Image現有條目的屬性設定為默認值。
如果您不想修改現有條目,那么您可能別無選擇,只能required從屬性中洗掉Image屬性(因為如果某些實體不提供值,則不需要屬性)并自己驗證新條目將 setter 設為Image私有并添加一個UpdateImage驗證新影像不為空并設定屬性的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/457049.html
