获取错误在asp.net mvc4中保存数据库中的更改时

时间:2014-03-05 20:40:32

标签: c# asp.net-mvc-4

enter image description here我的一个方法是我的模型的主键是'PlanetKey'

        Graph graph = new Graph();
        long lastGraphID = 1000;
        //graph.GraphID = lastGraphID;
        graph.ItemType = enumType;
        graph.GraphItemTitle = title;
        graph.GraphItemDescription = statusMessage;
        graph.GraphItemUserFullName = null;
        graph.GraphItemURL = url;
        graph.ItemSummary = title + ": " + statusMessage; ;
        graph.ItemCreatedOn = DateTime.UtcNow.ToLocalTime();
        //graph.GraphID = lastGraphID;
        graph.GraphCustomURL = null;
        graph.DbType = "OFFLINE";
        graph.ItemOwnerGraphID = ItemOwnerGraphID;
        graph.ItemUserID = userInfoId;
        graph.PRIMARYTaggedAcademicTreeNodeId = 0;
        graph.PRIMARYTaggedAcademicTreeSerialNumber = "1";
        graph.PRIMARYTaggedCareerTreeNodeId = 0;
        graph.PRIMARYTaggedCareerTreeSerialNumber = "1";
        graph.PRIMARYTaggedSkillTreeNodeId = 0;
        graph.PRIMARYTaggedSkillTreeSerialNumber = "1";
        graph.PRIMARYTaggedAcademicTreeNodeId = 0;
        graph.PRIMARYTaggedAcademicTreeSerialNumber = "1";
        graph.PRIMARYTaggedToolTreeNodeId = 0;
        graph.PRIMARYTaggedToolTreeSerialNumber = "1";
        graph.CountReactions = 0;
        graph.CountResponses = 0;
        graph.CountRatings = 0;
        graph.AverageRating = 0;
        graph.CountRatings = 0;
        graph.CountUses = 0;
        graph.CountViews = 0;
        graph.isReported = false;
        graph.isPSKverified = false;
        graph.isPSKbanned = false;
        graph.isPSKresource = false;
        graph.AccessAllowedCode = 0;
        graph.AgeRestrictionCode = loginUserCurrentAge;
        graph.isHidden = false;

        GraphsController GraphsControllerObject = new GraphsController();
        long returnGraphId = GraphsControllerObject.CreateGraphIdByModel(graph);

这是我要求的另一个

public long CreateGraphIdByModel(Graph graph)
        {
           try
           {
                if (ModelState.IsValid)
                {
                    db.Graphs.Add(graph);
                    db.SaveChanges();
                    return graph.GraphID;
                }
           }
           catch (DataException)
           {
                  ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
           }            
            return graph.GraphID;
        }

这是在db.SaveChanges()中向我显示的 我错误地使用这段代码我只是想使用模型作为mvc4的crud传递我的参数

enter image description here

1 个答案:

答案 0 :(得分:0)

要知道错误是什么:您需要查看内部异常,它会准确地告诉您问题所在。

您可以检查异常innerException属性。它将是一个带有错误列表的EF异常。

错误上写着Error converting datetime2 to datetime

实体框架将所有日期作为Datetime2处理,因此,如果数据库中的字段是Datetime,则可能是一个问题。填充所有日期字段并更改数据类型是最常见的解决方案

从这里复制:'datetime2' error when using entity framework in VS 2010 .net 4.0