locked
After Insert -> different DateTime Value ! RRS feed

  • Question

  • Hello

     

    I'm using ASP.Net WebAplication with EF 4.0 and SQL 2008 R2.

     

    I'm inserting 2 x the same DateTime value and in my table I got 2 x different values ?!

     

    I created 2 SQL Tables with the sames Columns

     

    SQL Table:

    MessageID
    Subject
    Message
    SendDate (DATETIME or DATETIME2 no difference for my problem)
    ReadDate (DATETIME or DATETIME2 no difference for my problem)
    SentFromUser
    SentToUser

     

    Insert :

     

    using (modelEntities.aaaRiskEntities db = new aaaRiskEntities())
      {
      DateTime now = DateTime.Now;
    
      modelEntities.PM pm = new PM();
    
      modelEntities.User fromUser = db.User.Where(u => u.UserID == fromUserID).FirstOrDefault();
      modelEntities.User toUser = db.User.Where(u => u.UserID == toUserID).FirstOrDefault();
    
      pm.Subject = subject;
      pm.Message = message;
      pm.FromUser = fromUser;
      pm.ToUser = toUser;
      pm.SendDate = now;
    
      db.AddToPM(pm);
    
      //Create Copy for Sended PM
      modelEntities.PM_Sended pm_sent = new PM_Sended();
    
      pm_sent.Subject = subject;
      pm_sent.Message = message;
      pm_sent.FromUser = fromUser;
      pm_sent.ToUser = toUser;
      pm_sent.SendDate = now;
    
      db.AddToPM_Sended(pm_sent);
    
    
    
      db.SaveChanges();
      }

     

    If I'm debug this code the pm.SendDate and the pm_send.SendDate are equals !

    But in my Database the Values are different ?!

    PM : 2011-07-08 11:37:45.067
    PM_SENT : 2011-07-08 11:37:28.100

     

    Query Profiler 

     

    exec sp_executesql N'insert [dbo].[PM]([Subject], [Message], [SendDate], [ReadDate], [SentFromUser], [SentToUser])
    values (@0, @1, @2, null, @3, @4)
    select [MessageID]
    from [dbo].[PM]
    where @@ROWCOUNT > 0 and [MessageID] = scope_identity()',N'@0 nvarchar(255),@1 nvarchar(max) ,@2 datetime2(7),@3 int,@4 int',@0=N'abbbbbbb',@1=N'bfdbdfbfdbfdbfdbfdbfdb',@2='2011-07-08 13:07:49.4692287',@3=4,@4=4
    
    
    exec sp_executesql N'insert [dbo].[PM_Sended]([Subject], [Message], [SendDate], [ReadDate], [SentFromUser], [SentToUser])
    values (@0, @1, @2, null, @3, @4)
    select [MessageSendedID]
    from [dbo].[PM_Sended]
    where @@ROWCOUNT > 0 and [MessageSendedID] = scope_identity()',N'@0 nvarchar(255),@1 nvarchar(max) ,@2 datetime2(7),@3 int,@4 int',@0=N'abbbbbbb',@1=N'bfdbdfbfdbfdbfdbfdbfdb',@2='2011-07-08 13:07:34.0313457',@3=4,@4=4
    

     

    The Query also show: There are differnet values ?!

     

    I tryed also this:

     

    string now = DateTime.Now.ToString();
    pm.SendDate = DateTime.Parse(now);
    pm_sent.SendDate = DateTime.Parse(now);
    

     

    No Difference.

     

    Please help :)

     

    Best Greetings

     

    Cyril Iselin

     

     

     


     

     



    Friday, July 8, 2011 2:21 PM

Answers

  • Hi Cyril,

    Welcome!

    According to your code, it seems you have setted the property's StoreGeneratedPattern="Computed", the DateTime was generated by database, so there are different. http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.storegeneratedpattern.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Diräkt Monday, July 11, 2011 2:15 PM
    Monday, July 11, 2011 7:45 AM
  • Hello Alan_Chen

     

    Thanks a lot for your reply !

     

    The "StoreGeneratedPattern" is set to "None" for all DateTime Types. This is correct !?

     

     

    Best Greetings

     

    Diräkt

     

     

    EDIT :

    I'm sorry I wrote ValidateMethods foreach Entity. In the newValidate method of PM I set the SendDate to DatetTime.Now()


    • Marked as answer by Diräkt Monday, July 11, 2011 2:15 PM
    Monday, July 11, 2011 8:59 AM

All replies

  • *push*

     

    Sorry for pushing ;)

    Friday, July 8, 2011 10:42 PM
  • Hi Cyril,

    Welcome!

    According to your code, it seems you have setted the property's StoreGeneratedPattern="Computed", the DateTime was generated by database, so there are different. http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.storegeneratedpattern.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Diräkt Monday, July 11, 2011 2:15 PM
    Monday, July 11, 2011 7:45 AM
  • Hello Alan_Chen

     

    Thanks a lot for your reply !

     

    The "StoreGeneratedPattern" is set to "None" for all DateTime Types. This is correct !?

     

     

    Best Greetings

     

    Diräkt

     

     

    EDIT :

    I'm sorry I wrote ValidateMethods foreach Entity. In the newValidate method of PM I set the SendDate to DatetTime.Now()


    • Marked as answer by Diräkt Monday, July 11, 2011 2:15 PM
    Monday, July 11, 2011 8:59 AM