C# 帮助在更新前验证 table 数据

C# Help to validate table data before updating

我正在开发一个 C# 方法,该方法接收包含以下值的 json object。

meetingid

o_agendaitem
o_legistarid
o_title

n_agendaitem
n_legistarid  
n_title

带有“o”的值表示来自数据库的记录。 带“n”的值表示来自 Web 表单的已编辑值。

我把它们放在一起传递给 C# 方法:

 var data = JSON.stringify({
            'meetingid': $scope.meetingId,
            'o_agendaitem': o_xref.AgendaItem,
            'o_legistarid': o_xref.LegistarID,
            'o_title': o_xref.Title,
            'n_agendaitem': n_xref.AgendaItem,
            'n_legistarid': n_xref.LegistarID,
            'n_title': n_xref.Title,

我无法以清晰简单的方式编写逻辑。

例如,假设我有以下数据:

44841   1   62704   Title#1
44841   2   62218   Title#2
44841   3   62663   Title#3
44841   4   62679   Title#4
44841   5   62709   Title#5

用户编辑数值,把1改成6,那么数据会变成:

44841   2   62218   Title#2
44841   3   62663   Title#3
44841   4   62679   Title#4
44841   5   62709   Title#5
44841   6   62704   Title#6

会议 ID 和议程项目是一个组合键。

我使用“o”议程项目值和会议 ID 来查找该记录,如果存在,将其删除,然后使用“n”值创建一个新条目。

但我需要检查会议 ID + "n" 议程项目是否也不存在。

示例:

44841   1   62704   Title#1
44841   2   62218   Title#2 < I edit this row, I want to change the 2 for a 6, but I made a mistake and instead entered a 5, then based on my current logic, I would be deleting a record I did not mean to delete. How can I add a new check to make sure that the user is made aware of this error?
44841   3   62663   Title#3
44841   4   62679   Title#4
44841   5   62709   Title#5

这是我到目前为止的尝试(我在不确定是否正确的部分添加了注释):

   public ActionResult UpdateXRefItem(Parameters data)
    {
        bool status = false;
        string message = "";

        using (Entities entities = new Entities())
        {
            //use the "o" value to find the record
            var oldRec = entities.XRef_WIP.Find(data.meetingId, data.o_agendaItem);

            if (oldRec != null)                {
                
                //I am not sure if I should remove the record here, or better do a few more checks. 
                //entities.XRef_WIP.Remove(oldRec);
               
                //use the "n" value combined with the meeting id to see if already exists
                var newVal = entities.XRef_WIP.Find(data.meetingId, data.n_agendaItem);
                
                //if the value from the form already exists, return a message to the user
                if (newVal != null)
                {
                    status = false;
                    message = "This Agenda Item already exists for this Cross-Reference List. Please verfy your data and try again.";
                }
                else{
                    
                //after removing the "old" record, and check that the combination of meeting id + agenda item
                //do not exist alreay, then create a new table entry. 
                //I cannot update agenda item because it is part of the composite key.
                //entities.XRef_WIP.Add(new XRefWIP
                //{
                //    MeetingID = data.meetingId,
                //    AgendaItem = data.n_agendaItem,
                //    LegistarID = data.n_legistarId,
                //    Title = data.n_title
                //});
                //status = true;
                //message = "Cross-Reference record has been succssfully updated.";
                //entities.SaveChanges();                       
                }
            }
        }
        return new JsonResult { Data = new { status = status, message = message } };
    }

我希望这有意义,并且有人愿意提供帮助来完成逻辑。我想我很接近,但我很难把我的想法放在一起。

谢谢, 埃拉斯莫

查看解决方案是否有效,想法是检查要添加的记录是否存在,如果存在,则阻止用户继续。

public ActionResult UpdateXRefItem(Parameters data)
{
    
    using (Entities entities = new Entities())
    {
        // First check if the item you are trying to add exists
        var currentRec = entities.XRef_WIP.Find(data.meetingId, data.n_agendaItem);
        
        // Stop the user from continueing with the transaction
        if (currentRec != null)     
            return new JsonResult { Data = new { status = false, message = "Record already exists." } };
            
        // Use the "o" value to find the record
        var oldRec = entities.XRef_WIP.Find(data.meetingId, data.o_agendaItem);

        // If it exists then delete it
        if (oldRec != null) {
            entities.XRef_WIP.Remove(oldRec);
            
        // Add new record
        entities.XRef_WIP.Add(new XRefWIP()
        {
            MeetingID = data.meetingId,
            AgendaItem = data.n_agendaItem,
            LegistarID = data.n_legistarId,
            Title = data.n_title
        });
                
        // Return a new result
        return new JsonResult { Data = new { status = true, message = "Success!" } };
    }