将更改保存到临时变量是成功的,因为在调试时我可以看到值发生变化,但是当我实际尝试将更改保存到数据库时,不会更改所需的字段。
我正试图改变“被调查者”字段的价值。到'关闭'
nemesysEntities db2 = new nemesysEntities();
report report = db2.reports.Find(investigation.report_id);
report_status rep = db2.report_status.Find(3);
report.report_status = rep;
report.report_status.description = "Closed";
report.report_status.id = 3;
db2.Entry(report).State= EntityState.Modified;
db2.SaveChanges();
存储报告的表称为报告,investig.report_id表示正在调查的报告的ID。因此,通过将报告ID作为值传递,该表应在报告表中找到报告,并通过以下代码行将操作状态的值更改为已关闭(操作的ID"已关闭&# 34;在数据库中是3)
report_status rep = db2.report_status.Find(3);
但是,未存储数据库中所需的更改。 任何想法??
这是报告的模型
namespace WebApplication2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Web;
public partial class report
{
public report()
{
this.investigations = new HashSet<investigation>();
HttpCookie _userId = System.Web.HttpContext.Current.Request.Cookies["User"];
string userId="";
nemesysEntities db = new nemesysEntities();
if(_userId!=null)
{
this.user_id = Convert.ToInt32(_userId["Id"]);
user temp=db.users.Find(user_id);
this.email = temp.email;
this.phone = temp.phone;
temp.ConfirmPassword = temp.password;
int i = temp.points;
i++;
temp.points = i;
db.Entry(temp).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
else
{
this.user_id = 13;
}
this.status_id = 1;
}
public int id { get; set; }
[Display(Name = "Date of near miss")]
public System.DateTime date { get; set; }
[Display(Name = "Location")]
public string location { get; set; }
[DisplayName("Type")]
public int type_id { get; set; }
[Display(Name = "Description")]
public string description { get; set; }
public int status_id { get; set; }
public int user_id { get; set; }
[Display(Name = "Email Address")]
public string email { get; set; }
[Display(Name = "Phone Number")]
public string phone { get; set; }
public int votes { get; set; }
public virtual ICollection<investigation> investigations { get; set; }
public virtual report_status report_status { get; set; }
public virtual report_type report_type { get; set; }
public virtual user user { get; set; }
}
}
这是调查员的模型。您可以在其中一个setter中看到我使用的方法与我尝试用来更改报告状态的方法相同。
namespace WebApplication2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Web;
public partial class investigation
{
public investigation()
{
HttpCookie _userId = System.Web.HttpContext.Current.Request.Cookies["User"];
if (_userId != null)
{
this.investigator_id = Convert.ToInt32(_userId["Id"]);
//5 is the primary key of "Being investigated" in type of action
this.action_id = 5;
this.cause = "";
this.date_of_action = new DateTime(2012, 1, 3);
}
}
public int id { get; set; }
[Display (Name="Formal Description")]
public string formal_description { get; set; }
[Display(Name = "Cause of Hazard")]
public string cause { get; set; }
[Display(Name = "Action Take")]
public int action_id { get; set; }
[Display(Name = "Date of Action")]
public System.DateTime date_of_action { get; set; }
public int investigator_id { get; set; }
int _report_id;
[Display(Name = "Report Id:")]
public int report_id
{
get
{
return _report_id;
}
set
{
_report_id = value;
nemesysEntities db = new nemesysEntities();
report report = db.reports.Find(this.report_id);
//2 is the primary key of "Being investigated" in report status
report_status rep = db.report_status.Find(2);
report.report_status = rep;
db.Entry(report).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
}
public virtual investigation_action investigation_action { get; set; }
public virtual report report { get; set; }
public virtual user user { get; set; }
}
}