none
Issue with date coloum if datetime filed in entity framework

    Question

  • Hi,

    I have one issue in entity framework i have one table which contains below structure

    

    I have set default value for "Mydate" filed is getdate() .so that means whenever a new record enters into table current datetime automatically insered into DB..i dont want to fill from code becuase in code it make take some time to execute the code so that time will not exact...

    Problem :now when i am trying to insert some values into this field using entity framework it is inserting "0001-01-01 00:00:00.0000000" this value into database.so please let me know how we can achieve it .

    NOTE:even i changed the property in edmx file for "Mydate" coloum StoreGeneratedPattern=Computed.

    Please do help

    Thanks

    Manoj Ruwali

    Tuesday, January 14, 2014 11:22 AM

Answers

  • Hello Manoj,

    >> it is inserting "0001-01-01 00:00:00.0000000" this value into database

    This is strange because I have made a test which has similar table schema:

    It also has a datetime2(7) type field and has set the default value to be getdae(). And setting StoreGeneratedPattern ="Computed" isthe right way and be aware that whenever you update the model you have to change this again.

    My codes for adding the new entity:

    OrderDetail od = new OrderDetail() { OrderDetailID = 2, OrderDetailCode = "2", OrderDetailName = "2", OrderID = 1 };
    
    
                    db.OrderDetails.Add(od);
    
    
                    db.SaveChanges();
    

    I do not give the DateTime property any value and the result in database is:

    You can have a try the way as mine and I use VS2012, .Net 4.5, Entity Framework 5 and SQLServer2012.

    If this does not work for you, please let me know.

    Regards.


    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.

    Wednesday, January 15, 2014 3:57 AM