我有EF课程OAFile和电子邮件。我正在从服务器加载电子邮件并存储在存储库中。然后,用户可以列出电子邮件和"关联"他们使用所选文件。
电子邮件对象先前已插入存储库,我想获取该文件的电子邮件数。
file.Emails.Add(email)
将电子邮件添加到文件对象,但不会保存。
file.Emails.Count
总是返回0。
正确保存email.FileID字段。
我错过了什么?如何通过调用file.Emails.Count来获取电子邮件的数量?
[HttpPost]
[ValidateInput(false)]
public ActionResult AssignEmail(int emailId, int assignedTo)
{
if (emailId > 0 && assignedTo > 0)
{
Email email = repository.Emails.FirstOrDefault(x => x.EmailID == emailId);
OAFile file = repository.OAFiles.FirstOrDefault(x => x.FileID == assignedTo);
if (email != null && file != null)
{
email.FileID = assignedTo;
email.AssignedByID = HttpContext.User.Identity.GetUserId();
file.Emails.Add(email);
repository.Save();
return Json(new { success = true }, JsonRequestBehavior.DenyGet);
}
else
{
return Json(new { success = false }, JsonRequestBehavior.DenyGet);
}
}
else
{
return Json(new { success = false }, JsonRequestBehavior.DenyGet);
}
}
电子邮件类:
public class Email
{
public Email()
{
Attachments = new HashSet<EmailAttachment>();
}
[Key]
public int EmailID { get; set; }
public string EmailIdentifier { get; set; }
public int MessageNumber { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public DateTime DateSent { get; set; }
public string Body { get; set; }
public int? FileID { get; set; }
public string AssignedByID { get; set; }
public string AssignedToID { get; set; }
public string EmailType { get; set; }
public ICollection<EmailAttachment> Attachments { get; set; }
[ForeignKey("FileID")]
public virtual OAFile OAFile { get; set; }
[ForeignKey("AssignedByID")]
public virtual AppUser AssignedBy { get; set; }
[ForeignKey("AssignedToID")]
public virtual AppUser AssignedTo { get; set; }
}
和OAFile类:
public class OAFile
{
public OAFile()
{
Services = new HashSet<Service>();
Documents = new HashSet<Document>();
Notes = new HashSet<Note>();
Forms = new HashSet<Form>();
Emails = new HashSet<Email>();
}
[Key]
public int FileID { get; set; }
[Required]
public int CompanyID { get; set; }
[Required]
[StringLength(14)]
public string OurFileName { get; set; }
[Required]
[StringLength(100)]
public string CompanyFileName { get; set; }
[Required]
public string AppUserId_Creator { get; set; }
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime CreatedOn { get; set; }
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Client Client { get; set; }
public virtual ICollection<Service> Services { get; set; }
public ICollection<Document> Documents { get; set; }
public ICollection<Note> Notes { get; set; }
public ICollection<Form> Forms { get; set; }
public ICollection<Email> Emails { get; set; }
public virtual Company Companies { get; set; }
[ForeignKey("AppUserId_Creator")]
public virtual AppUser AppUsers_Creator { get; set; }
}
编辑:我正在调用下面的file.Emails.Count语句来检查文件是否有与之关联的电子邮件。
public bool IsFileEmpty(int fileId)
{
bool isFileEmpty = true;
OAFile file = repository.FindOAFile(fileId);
if (file.Services.Count > 0 || file.Documents.Count > 0
|| file.Forms.Count > 0 || file.Notes.Count > 0 || file.CCTable != null || file.AccountingTable != null
|| file.Emails.Count > 0
)
{
isFileEmpty = false;
}
return isFileEmpty;
}
编辑2:我正在为另一个控制器(HomeController.cs)调用IsFileEmpty()方法,同时为搜索结果填充视图模型。如果文件为空,结果行将显示“删除”链接。
public ActionResult FilesSearch(FileSearchViewModel viewModel)
{
var files = !string.IsNullOrWhiteSpace(viewModel.Keyword) ? repository.FindOAFiles(viewModel.Keyword) : repository.OAFiles;
var sortedFile = files.Where(x => x.IsFileOpen).OrderByDescending(x => x.CreatedOn).ToList();
sortedFile.AddRange(files.Where(x => !x.IsFileOpen).OrderByDescending(x => x.CreatedOn));
var resultsList = new List<FileSearchResultsViewModel>();
foreach (OAFile file in sortedFile)
{
var resultsViewModel = new FileSearchResultsViewModel();
resultsViewModel.oAFile = file;
resultsViewModel.isFileEmpty = IsFileEmpty(file.FileID);
resultsList.Add(resultsViewModel);
}
return PartialView("_FilesSearch", resultsList);
}