locked
Entity Framework insert not setting TimeStamp correctly RRS feed

  • Question

  • Hi, 

    I am using Entity Framework to insert records in the database.
    My database is MySql

    
    You can see the structure. [videoTagTs] is set to CURRENT_TIMESTAMP

    Now the Model created automatically by Entity Framework is as below

    Now when I insert value into it, videoTagTs value gets inserted as NULL, whereas it should have CURRENT_TIMESTAMP, I don't know what wrong I am doing.
    Note that I am not setting videoTagTs value in my code as it should get added automatically. See below image 

    How to correct this error?

    Thanks.


    sujit

    • Moved by DotNet Wang Monday, May 30, 2016 1:59 AM From VB.NET Forum
    Saturday, May 28, 2016 6:43 PM

Answers

  • Hello,

    Have you considered letting the database do this via a trigger?

    http://stackoverflow.com/questions/1138928/mysql-set-current-date-in-a-datetime-field-on-insert


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, May 29, 2016 8:29 AM
  • Hi,

    If you use Database first, please refer to the following steps.

    1. go to the edmx file designer by clicking on your edmx file.

    2. locate your table and the property.

    3. Right-click the column in the table that you want to change and click on properties.

    4. The property window should then come up and you will see as one of the properties "StoredGeneratedProperty". Change that to computed.

    if you use code first, please add the following code on property named videoTagTs:

    [DatabaseGenerated(DatabaseGenerationOption.Computed)] 
    public Nullable<System.DateTime> VideoTagTs { get; set; }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by sujit1779 Thursday, June 2, 2016 3:31 AM
    Thursday, June 2, 2016 2:59 AM

All replies

  • What happens if you unselect the “Allow Null” option in database definition?

    Saturday, May 28, 2016 7:01 PM
  • Look up Computed column for EF or set the timestamp in the object yourself.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Saturday, May 28, 2016 7:10 PM
  • I have a similar table in which TimeStamp column is NOT NULL, and it inserts 0000-00.... 


    sujit

    Saturday, May 28, 2016 7:18 PM
  • Setting up timestamp in the object not possible as the database server is in different timezone and object is created in a windows desktop app hence both times are different.

    How to look up computed column in EF, I just started using EF this week?

    sujit

    Saturday, May 28, 2016 7:21 PM
  • How to look up computed column in EF, I just started using EF this week?

    Is there something wrong with Bing or Google?

    Saturday, May 28, 2016 7:24 PM
  • Hello,

    Have you considered letting the database do this via a trigger?

    http://stackoverflow.com/questions/1138928/mysql-set-current-date-in-a-datetime-field-on-insert


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, May 29, 2016 8:29 AM
  • Hi,

    If you use Database first, please refer to the following steps.

    1. go to the edmx file designer by clicking on your edmx file.

    2. locate your table and the property.

    3. Right-click the column in the table that you want to change and click on properties.

    4. The property window should then come up and you will see as one of the properties "StoredGeneratedProperty". Change that to computed.

    if you use code first, please add the following code on property named videoTagTs:

    [DatabaseGenerated(DatabaseGenerationOption.Computed)] 
    public Nullable<System.DateTime> VideoTagTs { get; set; }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by sujit1779 Thursday, June 2, 2016 3:31 AM
    Thursday, June 2, 2016 2:59 AM
  • Thanks, it worked. 
    But wanted one clarification. My approach is database first, so will the above change if I update my Model?
    Also what exactly "Computed" do, does it ignore inserting value for that column in the database? I just started using Entity Framework so may be I am asking very basic questions.

    Thanks.

    sujit

    Thursday, June 2, 2016 3:34 AM
  • Hi Karen,

    I tried adding trigger, but couldn't as I have goDaddy shared hosting and they don't allow triggers, so I can't test it out. But I think it will work as triggers are meant for that only. Thanks for the pointer.


    sujit

    Thursday, June 2, 2016 3:35 AM
  • Thanks, it worked. 
    But wanted one clarification. My approach is database first, so will the above change if I update my Model?
    Also what exactly "Computed" do, does it ignore inserting value for that column in the database? I just started using Entity Framework so may be I am asking very basic questions.

    Thanks.

    sujit

    You manually changed the edmx file that defines the model. The Computed at one time told EF to not take the property data for the DB table column and persist it to the DB table column leave it alone.

    They may have changed how EF works in more recent versions. Without you telling EF to not take the information in an entity's property and persist, then the trigger is not going to work, unless EF is told not to populate the table column from the entity/object's property for the table column.    

    Thursday, June 2, 2016 5:57 PM