0

enter image description hereThis my one method primaryKey of my model is '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);

this is another what i am calling to

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;
        }

this is showing me eeror in db.SaveChanges() what m i doing wrong with this code i just wanna pass my parameter using model as crud of mvc4

enter image description here

  • What error is it showing, exactly? – Joachim Isaksson Mar 05 '14 at 20:43
  • You should probably have some way to capture the _actual_ exception to get a better idea of what is happening rather than just displaying a generic message. – D Stanley Mar 05 '14 at 20:44
  • An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code –  Mar 05 '14 at 20:45
  • @VipsSaini "See the inner exception for details". Could you please add that info? – Joachim Isaksson Mar 05 '14 at 20:48
  • @DStanley i just want why i am getting this error, when i sent all the parameter as variable that time its all done but when changed variable as model i got this error, is this because of it is not generating the primary key? –  Mar 05 '14 at 20:49
  • @JoachimIsaksson i just want why i am getting this error, when i sent all the parameter as variable that time its all done but when changed variable as model i got this error, is this because of it is not generating the primary key? –  Mar 05 '14 at 20:49
  • 1
    We can't tell you _why_ you're getting the error until we know _what_ the error is! It could be anything from a formatting error to your database server exploding. – D Stanley Mar 05 '14 at 20:51
  • "Error converting data type datetime2 to datetime" is your actual error. In other words, something is most likely not correct in your model regarding at least one of the datetime fields. (ItemCreatedOn mapped with SqlDbType.DateTime2 but is actually a DateTime in the database?) – Joachim Isaksson Mar 05 '14 at 20:55

1 Answers1

0

To know what the error is: You need to look at the inner exception, it will tell you precisely what the problem is.

You can inspect the exceptions innerException property. It will be an EF exception with a list of errors.

The error says, Error converting datetime2 to datetime

Entity framework handles all the dates as a Datetime2, so, if you fields in the database are Datetime, this could be a problem. Populating all the date fields and changing the datatype, are the most commom solutions

Copied from here: 'datetime2' error when using entity framework in VS 2010 .net 4.0

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32