C# 从ASP.NET核心中的子表更新父表
我有两张桌子;工作时间表(父级)和工作班次(子级)。现在,当按下我的C# 从ASP.NET核心中的子表更新父表,c#,sql-server,entity-framework,asp.net-core,asp.net-core-mvc,C#,Sql Server,Entity Framework,Asp.net Core,Asp.net Core Mvc,我有两张桌子;工作时间表(父级)和工作班次(子级)。现在,当按下我的WorkShiftBid查看页面中的按钮时,我可以将WSBidStatus更新为“已批准” 我在两个表中都有一个状态列(WorkScheduleStatus&WSBidStatus),我想要的是,例如,当按下按钮时,两个表中的WorkScheduleID都将更新为“已批准” 我的控制器应该如何执行此操作 型号: namespace Website.Models { public class WorkScheduleMod
WorkShiftBid
查看页面中的按钮时,我可以将WSBidStatus
更新为“已批准”
我在两个表中都有一个状态列(WorkScheduleStatus
&WSBidStatus
),我想要的是,例如,当按下按钮时,两个表中的WorkScheduleID
都将更新为“已批准”
我的控制器应该如何执行此操作
型号:
namespace Website.Models
{
public class WorkScheduleModel
{
[Key]
public Guid WorkScheduleID { get; set; }
public string WorkScheduleStatus { get; set; }
}
public class WorkShiftBidModel
{
[Key]
public Guid WorkShiftBidID { get; set; }
public string WSBidStatus { get; set; }
public Guid WorkScheduleID { get; set; }
[ForeignKey("WorkScheduleID")]
public WorkScheduleModel WorkSchedules { get; set; }
}
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> WorkShiftBid(Guid? id)
{
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlDataReader dr;
connectionString();
con.Open();
com.Connection = con;
com.CommandText = "update WorkShiftBid set WSBidStatus ='Approved' where WorkShiftBidID = @id ";
com.Parameters.AddWithValue("@id", id);
dr = com.ExecuteReader();
var workShiftBidModel = await _context.WorkShiftBid.FindAsync(id);
if (dr.Read())
{
con.Close();
return RedirectToAction(nameof(WorkShiftBid));
}
else
{
con.Close();
return RedirectToAction(nameof(WorkShiftBid));
}
void connectionString()
{
con.ConnectionString = ".........................";
}
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务WORKSHIFT BID(Guid?id)
{
SqlConnection con=新的SqlConnection();
SqlCommand com=newsqlcommand();
SqlDataReader-dr;
连接字符串();
con.Open();
com.Connection=con;
com.CommandText=“更新WorkShiftBid集WSBidStatus='Approved',其中WorkShiftBidID=@id”;
com.Parameters.AddWithValue(“@id”,id);
dr=com.ExecuteReader();
var workShiftBidModel=wait _context.WorkShiftBid.FindAsync(id);
if(dr.Read())
{
con.Close();
返回重定向到操作(名称(工作班次投标));
}
其他的
{
con.Close();
返回重定向到操作(名称(工作班次投标));
}
无效连接字符串()
{
con.ConnectionString=“…………”;
}
}
视图:
@model IEnumerable
.
.
.
}
我没有足够的声誉发表评论,但您是否同时使用实体框架和SQLCommand
但是,您可以找到父密钥并通过SQL命令进行更新,如下所示:
UPDATE WorkScheduleModel SET Status = 'Approved' WHERE Id = {ID}
用父记录的键替换{Id}
UPDATE WorkScheduleModel SET Status = 'Approved' WHERE Id = {ID}