我有一个带有实体框架 4.1 的简单 asp.net mvc3 项目。我问了一个先前的问题,答案很好 -如何正确覆盖 EF 4.1 中的 SaveChanges 函数 date 和 loggedby 的值是在类构造函数中设置的。这对于 Create 操作很好,但是当我使用 Edit 操作时,我看到日期和 LoggedBy 字段已更改,这是我不想要的。我还看到 Delete 不起作用 - 我猜这是因为构造函数。如何解决此问题,以便为 Create 设置 Date 和 LoggedBy 值,但在 Edit 期间不更改?B.
using System;
using System.Collections.Generic;
using System.Web;
public partial class tblDefectDetail
{
public tblDefectDetail()
{
DateLogged = DateTime.Now;
LoggedBy = HttpContext.Current.User.Identity.Name;
}
public int ID { get; set; }
public int Project { get; set; }
public string LoggedBy { get; set; }
public System.DateTime DateLogged { get; set; }
public string Issue { get; set; }
我的动作如下所示:
public ActionResult Create()
{
ViewBag.BugLocation = new SelectList(db.tblBugLocations, "ID", "BugLocation");
ViewBag.BugType = new SelectList(db.tblBugTypes, "ID", "BugType");
ViewBag.BusinessUnit = new SelectList(db.tblBusinessUnits, "ID", "BU_Name");
ViewBag.BugFoundMethod = new SelectList(db.tblDefectDiscoveryMethods, "ID", "DiscoveryMethod");
ViewBag.Project = new SelectList(db.tblProjects, "ID", "ProjectName");
ViewBag.Language = new SelectList(db.tblLanguages, "ID", "Language");
return View();
}