C# SetValues,EntityState.Modified 不工作

C# SetValues, EntityState.Modified not working

我的数据库中有以下条目:

MeetingID   AgendaItem  LegistarID  Title
48620       3           60710       Comment
48620       5           60615       Extending report date
48620       6           60714       Update on Additional meeting dates
48620       7           59909       Budget Rules & Procedures
48620       8           60703       Update Director name

我需要更新这些值:

MeetingID   AgendaItem  LegistarID  Title
48620       3           60710       Public Comment
48620       5           60769       Briefing by Victor
48620       6           60615       Extending report dates
48620       7           60714       Update on Additional meeting dates
48620       8           60703       Update from Director on new processes

我在 C# 中尝试执行此操作的方式如下:

if (ModelState.IsValid)
{
    var errors = new List<string>();
    var rowCounter = 1;

    using (Entities db = new Entities())
    {
        foreach (var i in meeting)
        {
            if (i.MeetingID == 0)
            {
                // Let the user know this row is bad 
                errors.Add($"Row {rowCounter}: Missing Meeting ID value. " +
                            "Verify that the data you are trying to upload meets the required criteria, " +
                            "and then try to upload your file again." );
                break;
            }
            // Check if LegistarID is missing
            if (i.LegistarID == 0)
            {
                // Check if Agenda Item is present
                if (i.AgendaItem == 0)
                {
                    errors.Add($"Row {rowCounter}: Meeting has no LegistarID and no Agenda Item. Please check data.");
                    break;
                }
                else
                {
                    i.LegistarID = i.AgendaItem;
              }
            }

            var compositeKey = db.Meeting.Find(i.MeetingID, i.AgendaItem);

            if (compositeKey == null)
            {

                // Add new
                db.Meeting.Add(i);

            }
            else
            {
                // Serves as an update, or addition of a previously imported dataset
                db.Entry(compositeKey).CurrentValues.SetValues(i.MeetingID);
                db.Entry(compositeKey).CurrentValues.SetValues(i.AgendaItem);
                db.Entry(compositeKey).CurrentValues.SetValues(i.LegistarID);
                db.Entry(compositeKey).CurrentValues.SetValues(i.Title);
                db.Entry(compositeKey).State = EntityState.Modified;
            }
            rowCounter++;
        }
        // If there are errors do not save and return error message
        if (errors.Count > 0)
        {
            return new JsonResult { Data = new { status = false, message = string.Join("\n", errors) } };
        }
        db.SaveChanges();
        status = true;
    }
}
else
{
    message = string.Format(@"Please, verify that the file you are trying to upload is correctly formatted, 
                            and that the data it contains, meets the expected criteria, 
                            then click the upload button again. \n Thank you!");
    return new JsonResult { Data = new { status = status, message = message } };
}

添加部分的代码运行良好,但如果找到组合键则更新记录的部分不起作用,更新不起作用。

我不确定我这样做是否是最好的方法,但是如果有更好的方法我愿意更改代码,或者如果我在执行该过程时遇到错误,请告诉我知道

感谢任何帮助。

谢谢, 埃拉斯莫

删除您对 SetValues 的所有调用并将其替换为单个调用:

db.Entry(compositeKey).CurrentValues.SetValues(i);

SetValues 接受对象作为参数,根据对象属性名称将数据复制到实体:

Any property on the object with a name that matches a property name in the entity type and can be read will be copied. Other properties will be ignored.