C# 创建了重复记录,将更改保存到实体框架6中的数据库
在Visual Studio 2013 web express中使用MVC 5、EF 6.01和Identity 我认为这将是一个简单的任务,添加一个历史记录表来跟踪定价的变化。对于父记录的每次保存,将创建两个子历史记录,但不确定原因。我尝试了下面的方法,也没有使用集合(单独更新的独立历史表)。还尝试更改控制器中事件的顺序C# 创建了重复记录,将更改保存到实体框架6中的数据库,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,在Visual Studio 2013 web express中使用MVC 5、EF 6.01和Identity 我认为这将是一个简单的任务,添加一个历史记录表来跟踪定价的变化。对于父记录的每次保存,将创建两个子历史记录,但不确定原因。我尝试了下面的方法,也没有使用集合(单独更新的独立历史表)。还尝试更改控制器中事件的顺序 private AppDb db = new AppDb(); private UserManager<IdentityUser> userManager =
private AppDb db = new AppDb();
private UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
public class Threshold
{
public int ThresholdId { get; set; }
public string ItemId { get; set; }
public string OfficeCode { get; set; }
public string Unit { get; set; }
public string Type { get; set; }
public string Color { get; set; }
public string Frequency { get; set; }
public decimal Price { get; set; }
public decimal OldPrice { get; set; }
public int? Volume { get; set; }
public int? Sort { get; set; }
public DateTime? DateModified { get; set; }
public virtual ICollection<Threshold_History> Threshold_History { get; set; }
}
public class Threshold_History
{
public int Threshold_HistoryId { get; set; }
public int ThresholdId { get; set; }
public decimal PriceFrom { get; set; }
public decimal PriceTo { get; set; }
public DateTime DateCreated { get; set; }
public string UserId {get; set;}
public virtual Threshold Threshold { get; set; }
}
private-AppDb=new-AppDb();
private UserManager UserManager=new UserManager(new UserStore());
公共类阈值
{
public int ThresholdId{get;set;}
公共字符串ItemId{get;set;}
公共字符串OfficeCode{get;set;}
公共字符串单元{get;set;}
公共字符串类型{get;set;}
公共字符串颜色{get;set;}
公共字符串频率{get;set;}
公共十进制价格{get;set;}
公共十进制OldPrice{get;set;}
公共int?卷{get;set;}
公共int?排序{get;set;}
公共日期时间?日期修改{get;set;}
公共虚拟ICollection阈值\u历史记录{get;set;}
}
公共类历史记录
{
public int Threshold_HistoryId{get;set;}
public int ThresholdId{get;set;}
来自{get;set;}的公共十进制价格
公共十进制价格{get;set;}
public DateTime DateCreated{get;set;}
公共字符串用户标识{get;set;}
公共虚拟阈值{get;set;}
}
控制员:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ThresholdId,ItemId,OfficeCode,Unit,Type,Color,Frequency,Price,Volume,Sort,OldPrice")] Threshold threshold)
{
if (ModelState.IsValid)
{
db.Entry(threshold).State = EntityState.Modified;
//update history table
Threshold_History hist = new Threshold_History();
List<Threshold_History> histories = new List<Threshold_History>();
hist.ThresholdId = threshold.ThresholdId;
hist.PriceFrom = threshold.OldPrice;
hist.PriceTo = threshold.Price;
hist.DateCreated = DateTime.Now;
hist.UserId = User.Identity.GetUserId();
histories.Add(hist);
threshold.DateModified = DateTime.Now;
threshold.Threshold_History = histories;
db.SaveChanges();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果编辑([Bind(Include=“ThresholdId,ItemId,OfficeCode,Unit,Type,Color,Frequency,Price,Volume,Sort,OldPrice”)]阈值阈值)
{
if(ModelState.IsValid)
{
db.Entry(阈值).State=EntityState.Modified;
//更新历史记录表
Threshold_History hist=新的Threshold_History();
列表历史=新列表();
hist.ThresholdId=threshold.ThresholdId;
hist.PriceFrom=threshold.OldPrice;
hist.PriceTo=阈值价格;
hist.DateCreated=DateTime.Now;
hist.UserId=User.Identity.GetUserId();
历史。添加(历史);
threshold.DateModified=DateTime.Now;
threshold.threshold_History=历史记录;
db.SaveChanges();
返回Json(新的{success=true},JsonRequestBehavior.AllowGet);
}
}
注意:调试器中的每个步骤都必须单击两次。这几乎就像两个代码实例同时运行一样。跨接时,其他模块和类可以正常工作
EDIT可添加调用EDIT的方式。它来自模式对话框中的ajax调用:
//modal functions
function editItem(e){
e.preventDefault();
$('#dialogContent').load(this.href, function () {
$('#dialogDiv').modal({
backdrop: 'static',
keyboard: true
}, 'show');
bindForm(this);
});
return false;
};
function bindForm(dialog) {
$('form', dialog).submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
$('#dialogDiv').modal('hide');
loadTab(tabRef);
} else {
$('#dialogContent').html(result);
$('#dialogDiv').modal({
backdrop: 'static',
keyboard: true
}, 'show');
bindForm();
}
},
error: function (requestObject, error, errorThrown) {
$('#modalError').html("<br><p>" + errorThrown + "</p> ");
}
});
return false;
});
}
function validateModal() {
var form = $(".modal-form");
$.validator.unobtrusive.parse(form);
if (form.valid()) {
form.submit();
}
}
//模态函数
功能编辑项(e){
e、 预防默认值();
$('#dialogContent').load(this.href,函数(){
$('#dialogDiv').modal({
背景:“静态”,
键盘:正确
}"show";;
bindForm(本);
});
返回false;
};
函数绑定窗体(对话框){
$('form',dialog).submit(函数(){
$.ajax({
url:this.action,
类型:this.method,
数据:$(this).serialize(),
成功:功能(结果){
如果(结果、成功){
$('dialogDiv').modal('hide');
loadTab(tabRef);
}否则{
$('#dialogContent').html(结果);
$('#dialogDiv').modal({
背景:“静态”,
键盘:正确
}"show";;
bindForm();
}
},
错误:函数(请求对象、错误、错误抛出){
$(“#modalError').html(“
”+errorshown+””);
}
});
返回false;
});
}
函数validateModel(){
变量形式=$(“.model形式”);
$.validator.unobtrusive.parse(表单);
if(form.valid()){
表单提交();
}
}
是否从表单上的提交按钮调用validateModel()
?我没有看到任何preventDefault
或returnfalse
,因此提交按钮很可能会同时调用validateModel
,这会导致表单被发布,并且提交按钮仍然执行其默认功能:发布表单。这就解释了重复呼叫的原因
如果您自己处理提交,则应将提交按钮的类型设置为“按钮”,以防止其自动提交。是否从表单上的提交按钮调用
validateModel()
?我没有看到任何preventDefault
或returnfalse
,因此提交按钮很可能会同时调用validateModel
,这会导致表单被发布,并且提交按钮仍然执行其默认功能:发布表单。这就解释了重复呼叫的原因
如果您自己处理提交,则应将提交按钮的类型设置为“按钮”,以防止其自动提交。是否从表单上的提交按钮调用
validateModel()
?我没有看到任何preventDefault
或returnfalse
,因此提交按钮很可能会同时调用validateModel
,这会导致表单被发布,并且提交按钮仍然执行其默认功能:发布表单。这就解释了重复呼叫的原因
如果您自己处理提交,则应将提交按钮的类型设置为“按钮”,以防止其自动提交。是否从表单上的提交按钮调用
validateModel()
?我没有看到任何preventDefault
或returnfalse
,因此提交按钮很可能会同时调用validateModel
,这会导致表单被发布,并且提交按钮仍然执行其默认功能:发布表单。这就解释了重复呼叫的原因
如果您自己处理提交,y