使用实体框架将数据保存到SQL Server db时,日期值更改为前一天 [英] While using entity framework for saving data to SQL server db, date value changes to previous day

查看:82
本文介绍了使用实体框架将数据保存到SQL Server db时,日期值更改为前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架5将数据从应用程序保存到带有webservice的SQL Server 2014.它是一个winforms应用程序。



实体类声明日期如下所示



I am using entity framework 5 for saving data from application to SQL Server 2014 with a webservice.It is a winforms Application.

entity class is having date declared as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.Commen;
namespace Entities.Masters
{
   public class ChittyRecieptEntity:Utility
    {
        public DateTime ChittyRepaymentDate { get; set; }
       ///...........
       ///.........
    }
}





和Web服务类创建如下





and web services class is created like below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities.Masters;
using System.Data.Entity;
using Data;
namespace Operations.Masters
{
   public class ChittyRecieptOperations
    {
       public ChittyRecieptEntity DoSaveChittyReciept(ChittyRecieptEntity entity)
        {
            try
            {
                using (JMFLEntities ctxt = new JMFLEntities())
                {
                    TblChittyReciept tblobj = new TblChittyReciept();
                ///.....
                ///....
                    tblobj.ChittyRepaymentDate = entity.ChittyRepaymentDate;
                    ctxt.SaveChanges();
                 }
            }
            catch (Exception ex)
            {
            }
            return entity;
        }





数据库字段是ChittyRepaymentDate,声明为date而非null



服务器在美国(godaddy)



此日期字段从应用程序传递(使用datetime.now),但在sql上保存日期字段后服务器它保存为以前的日期值。



你能告诉我我做错了什么。



提前致谢

George



我的尝试:



在使用本地服务器上的本地计算机和数据库服务时,日期正确保存。



a在应用程序保存日期选择器中选择的上一个日期正确



选择datepicker上的日期两次(当前日期除当前日期之外)

正确保存



从具有datetime.now的应用程序传递的日期和日期选择器的默认日期(当前日期)保存不正确或上一个日期<在调试器中使用断点时,可以看到从应用程序传递的日期值是正确的,并且在实时服务器上保存不正确。



database field is ChittyRepaymentDate ,declared as date and not null

server is in US (godaddy)

this date field is passing from application (using datetime.now) ,but after saving in date field on sql server it is saved as previous date value.

can you tell me what might i have done wrong.

Thanks in advance
George

What I have tried:

while using service from local machine and database on live server, date is saving correctly.

a previous date selected in date picker of application saves correctly

selecting date on datepicker twice (other than current date then current date)
saves correctly

date passing from application with datetime.now and default date of date picker(current date) saves incorrect or previous date

while using break point in debugger can see value of date passing from application is correct and saving is incorrect on live server.

推荐答案



您是否尝试调用context.AcceptAllChanges()?

以下链接 c# - 实体框架中的SaveChanges与AcceptAllChanges - 堆栈溢出 [ ^ ]包含两种方法的用途。



Hi,
Did you try to call context.AcceptAllChanges() ?
The following link c# - SaveChanges vs. AcceptAllChanges in Entity Framework - Stack Overflow[^] contains the use for both methods.

using (JMFLEntities ctxt = new JMFLEntities())
                {
                    TblChittyReciept tblobj = new TblChittyReciept();
                ///.....
                ///....
                    tblobj.ChittyRepaymentDate = entity.ChittyRepaymentDate;
//Edit: as you're creating a new EF object, I think you should attach it to context such as: 
ctxt.TblChittyReciepts.AddObject(tblobj); 

                    ctxt.SaveChanges();
                    ctxt.AcceptAllChanges(); 
                 }


我已经通过在应用程序中使用时区传递转换时间来解决了这个问题。因为应用程序的用法在India.I已经使用过这个



I have resolved the issue by passing converted time using timezone within the application.Since the application's usage is within India.I have used like this

public static DateTime toISTDate(DateTime input)
       {
           return TimeZoneInfo.ConvertTime(input.Date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
       }





和转换日期输入为





and converted date input as

Entity.ChittyRepaymentManualDate = FormUtility.toISTDate(dtpManualRcptDate.Value);





这解决了我的问题。我认为在使用实体框架时,从应用程序传递的datetime.now的值已更改以托管空间位置为基础的时间。



This resolved my issue.I think while using entity framework,the value of datetime.now passing from application is changed to the hosted spaces location based time.


这篇关于使用实体框架将数据保存到SQL Server db时,日期值更改为前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆