none
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

All replies