C# asp.net控制器:如何保存“a”的数据;历史";是否在删除具有类似信息的表的同时删除表?
我正在尝试为我的网站创建一个历史页面/表格,当用户完成一项训练时,当前用户训练表格会在完成后删除训练信息(该信息已在运行),历史记录表会用相同的字段+日期时间进行更新,这样用户就可以看到他们以前完成的训练 目前,我对如何实现这一点感到困惑。我当前在控制器中的“完成”功能如下所示:C# asp.net控制器:如何保存“a”的数据;历史";是否在删除具有类似信息的表的同时删除表?,c#,sql,asp.net,asp.net-mvc,visual-studio,C#,Sql,Asp.net,Asp.net Mvc,Visual Studio,我正在尝试为我的网站创建一个历史页面/表格,当用户完成一项训练时,当前用户训练表格会在完成后删除训练信息(该信息已在运行),历史记录表会用相同的字段+日期时间进行更新,这样用户就可以看到他们以前完成的训练 目前,我对如何实现这一点感到困惑。我当前在控制器中的“完成”功能如下所示: [HttpPost, ActionName("Complete")] [ValidateAntiForgeryToken] public ActionResult Com
[HttpPost, ActionName("Complete")]
[ValidateAntiForgeryToken]
public ActionResult CompleteConfirmed(int id)
{
UserCurrWorkout userCurrWorkout = db.UserCurrWorkouts.Find(id);
db.UserCurrWorkouts.Remove(userCurrWorkout);
db.SaveChanges();
return RedirectToAction("Index");
}
这只是用于根据ID删除表中信息的CRUD代码。我希望添加的内容非常简单,如下所示:
var oldWorkouts = new UserWorkoutHistory { UserId = model.UserId, UserOldWorkout = model.UserActiveWorkout };
db.UserWorkoutHistories.Add(oldWorkouts);
就在db.UserCurrWorkouts.上方。删除code。我当前训练数据库的“模型”在函数中未被识别,但是,如何使其如此是我的主要问题,除非有更好的方法
任何信息或提示将不胜感激!让我知道,如果你想看到任何其他代码,可能会带来清晰的我的问题
编辑:以下是UserCurr训练模型:
namespace Powerlevel.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("UserCurrWorkout")]
public partial class UserCurrWorkout
{
[Key]
public int UCWId { get; set; }
public int UserId { get; set; }
public int UserActiveWorkout { get; set; }
public virtual User User { get; set; }
public virtual WorkoutExercise WorkoutExercise { get; set; }
}
}
和UserWorkAuthory历史模型:
namespace Powerlevel.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("UserWorkoutHistory")]
public partial class UserWorkoutHistory
{
[Key]
public int UWHId { get; set; }
private DateTime Date = DateTime.Now;
public DateTime CurrentTime
{
get { return Date; }
set { Date = value; }
}
public int UserId { get; set; }
public int UserOldWorkout { get; set; }
public virtual User User { get; set; }
public virtual WorkoutExercise WorkoutExercise { get; set; }
}
}
如果我没弄错你的意思,我想这就是你需要的:
[HttpPost, ActionName("Complete")]
[ValidateAntiForgeryToken]
public ActionResult CompleteConfirmed(int id)
{
UserCurrWorkout userCurrWorkout = db.UserCurrWorkouts.Find(id);
var oldWorkouts = new UserWorkoutHistory { UserId = userCurrWorkout.UserId, UserOldWorkout = userCurrWorkout.UserActiveWorkout };
db.UserWorkoutHistories.Add(oldWorkouts);
db.UserCurrWorkouts.Remove(userCurrWorkout);
db.SaveChanges();
return RedirectToAction("Index");
}
更新:
此外,old workout
的外键WorkoutExercise\u LinkId
也必须分配到此行中:
var oldWorkouts = new UserWorkoutHistory { UserId = userCurrWorkout.UserId, UserOldWorkout = userCurrWorkout.UserActiveWorkout , WorkoutExerciseId = 'a valid key from WorkoutExercise table'};
更新2:
不过,首先需要在usercurrcout
实体中定义外键,并避免EF在数据库中生成实体中不存在的fk列:
[Table("UserCurrWorkout")]
public partial class UserCurrWorkout
{
[Key]
public int UCWId { get; set; }
public int UserId { get; set; }
public int UserActiveWorkout { get; set; }
public virtual User User { get; set; }
public int WorkoutExerciseId { get; set; } // The explicit foreign key
public virtual WorkoutExercise WorkoutExercise { get; set; }
}
你好像做了那件事!你的问题是什么?对不起!我说得不准确,这就是我想要做的,但是,我的CurrWorkout db的模型在该函数中没有被识别,我希望我可以添加一些东西来建立这种连接。请提供UserCurrWorkout
和UserWorkAuthHistory
模型类。你真的需要第二个表吗?也许只需将WorkoutCompleteDate添加为null即可。活动训练是db.UserCurrWorkouts.Where(wo=>wo.WorkoutCompleteDate!=null)
这就是为什么他们称之为视图:)设计基础表以实现效率和正常性,然后根据需要呈现。例如,对于未结发票和已结发票,没有单独的表。代码运行时没有问题,但当我尝试在两个表上执行删除/保存过程时,我在站点上不断收到错误,消息为异常详细信息:System.Data.SqlClient.SqlException:列名“WorkoutExercise\u LinkId”无效。
两个表中的WorkoutExercise表以完全相同的方式链接,并且根本不会被更改,因此我不确定当我到达此点时,为什么它无法识别列名您有外键您的UserWorkHistory
表名为WorkoutExercise\u LinkId
,在启动oldWorkouts
行中的var oldWorkouts=new UserWorkoutHistory{UserId=userCurrWorkout.UserId,UserOldWorkout=userCurrWorkout.UserActiveWorkout}时,必须从相应的表中为该表分配一个有效键
这就是我困惑的地方,我没有任何名为WorkoutExercise\u LinkId
,我有一个名为WorkoutExercise
的表,其中包含一个LinkId
值,UserCurrWorkouts
成功连接到该值。如果我尝试向我的控制器键入WorkoutExercise\u LinkId=…
,它会出现错误,因为它不存在。我尝试了var oldWorkouts=new UserWorkoutHistory{UserId=userCurrWorkout.UserId,UserOldWorkout=userCurrWorkout.UserActiveWorkout,WorkoutExercise=userCurrWorkout.WorkoutExercise}代码>但这导致了前面的错误。不过我很感激你帮我度过这段时间!原因是您尚未在UserWorkoutHistory
模型中为WorkoutExercise
定义显式外键,因此EF已为数据库中的关系生成一个名为WorkoutExercise\u LinkId
的列。最佳做法是使用新的迁移重新创建数据库,并定义显式外键,避免EF生成实体中不存在的列。我最终使用布尔标记更改了CurrWorkout表,正如@SteveGreene所建议的,根据我目前的知识,处理和编码似乎更容易。再次感谢!