Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找EF对象中的版本更改_C#_Asp.net Mvc_Entity Framework_Version Control - Fatal编程技术网

C# 查找EF对象中的版本更改

C# 查找EF对象中的版本更改,c#,asp.net-mvc,entity-framework,version-control,C#,Asp.net Mvc,Entity Framework,Version Control,我有一个令人尴尬地失控的问题 我正在使用MVC和EF,并且有一个用户可以编辑的模型。我刚刚得到一个新的要求,如果它被编辑,被编辑的属性将在视图中显示为红色,以供其他用户查看 我的模型有很多属性,但主键是GUID,并且模型可以有多个ParentID。因此,对于一个给定的GUID和ParentID,我想找到哪些属性已经更改 到目前为止,这感觉是错误的,我有一些jquery在我看来,它做了以下几点 $("[name*='Request.']") .not(":hidden")

我有一个令人尴尬地失控的问题

我正在使用MVC和EF,并且有一个用户可以编辑的模型。我刚刚得到一个新的要求,如果它被编辑,被编辑的属性将在视图中显示为红色,以供其他用户查看

我的模型有很多属性,但主键是GUID,并且模型可以有多个ParentID。因此,对于一个给定的GUID和ParentID,我想找到哪些属性已经更改

到目前为止,这感觉是错误的,我有一些jquery在我看来,它做了以下几点

$("[name*='Request.']")
        .not(":hidden")
        .each(function () {
            var $this = $(this);
            $.ajax({
                url: '@Url.Action("IsChanged", "Shared")',
                data: { fieldName: $this.attr("name"), parentID: @Model.Request.ID },
                success: function (data) {
                    if (data) {
                        if ($this.is(":radio")) {
                            $this.parent().addClass("isChanged");
                        } else
                            $this.addClass("isChanged");
                    }
                }
            });
        });
如果我尝试从ajax函数返回true,那么这个脚本工作得很好。然而,问题在于ajax函数。如何找到更改?到目前为止,我得到的是:

public ActionResult IsChanged(string fieldName, int parentID)
    {
        using (MyEntities _db = new MyEntities())
        {
            string[] aName = fieldName.Split('.');

            // Count the number of version
            int versionCount = _db.SubRequests.Count(r => r.ParentID == parentID);
            // Get the first version
            SubRequest sr = _db.SubRequests.FirstOrDefault(r => r.ParentID == parentID);
            // Check if the count is the same for the specific value
            int changeCount = _db.ExecuteStoreQuery<int>("select count(parentID) from "+aName[0]+" where parentID = " + parentID + " and " + aName[1] +"='{0}'", THEVALUEINORIGINAL);


            bool isChanged = changeCount == versionCount;

            return Json(isChanged, JsonRequestBehavior.AllowGet);
        }
    }
到目前为止,我所做的感觉不对劲,显然这不起作用,但我迷路了


让我知道这是可以理解的,或者如果你需要更多的信息。我应该放弃这个想法,构建完全不同的东西吗?或者有什么方法可以拯救我吗?

看起来您可能想看看如何正确处理并发冲突。看看这篇文章。

我肯定这不是最漂亮的方法,但我想我会和大家分享我最终决定的解决方案,以防其他人也会想做类似的事情

我的jQuery ajax函数保持不变,但在控制器中有以下功能:

public ActionResult IsChanged(string fieldName, int parentID)
{
    using (MyEntities _db = new MyEntities())
    {
        bool isChanged = false; 

            string[] aName = fieldName.Split('.');

            string strTableName = aName[0];
            string strFieldName = aName[1];

            string strQuery = String.Format("select distinct {0} from {1} where parentID = {2}", strFieldName, strTableName, parentID);

            isChanged = _db.ExecuteStoreQuery<dynamic>(strQuery).Count() > 1;

            return Json(isChanged, JsonRequestBehavior.AllowGet);
    }
}

对我来说很好。然而,这是一个负载相当重的页面,包含所有这些ajax调用。

谢谢,但这不是我想要的。我没有并发问题。相反,我有几个版本,并且想要一个简单的方法来查找属性是否已更改。您能详细说明一下您的db模式是什么样子吗?很难说有什么数据可以查询。