ASP.NET MVC5将2个同步时间添加到应用程序

时间:2017-07-18 23:35:07

标签: model-view-controller entity-framework-6 sqldb

我将2个同步时间添加到我的数据库中作为2个新列并插入值如下:

USE [DB]


ALTER TABLE [dbo].[TableName]
    ADD ColumnName2 time, ColumnName3 time

这是为了添加列。

为了插入我所做的行值:

USE DB

INSERT INTO TableName (ColumnName2, ColumnName3)
VALUES ('20:30:00', '23:30:00')

这是这些列的行中固定时间的数据。

我还浏览了应用程序的所有层,例如(控制器,模型,视图,查询,服务,接口等等。现在,当我尝试更新任何新的时间时,它们默认为第一次已经存在于桌面上的COLUMN,其中包含时间。

我无法从应用程序发布时间字段的图像,因为它是不允许的。但是,图像位于一个小面板中,由3个字段(textboxfor)组成,每个字段都有一个时间选择器。

非常感谢任何帮助。

谢谢

现在我想我会发布一些示例代码,看看是否有帮助

//我的控制器方法用于那些同步时间

[HttpPost]
        [Page(PageName.UpdateSystemConfigTime)]
        public ActionResult UpdateTime(SystemMaintenanceViewModel model)
        {
            var dateTime = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime2 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime2, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime3 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime3, "h:mm tt", CultureInfo.InvariantCulture);                        
            //model.ProcessTime
            if (model.SystemConfiguration.SynchronizationTime != null &&
                model.SystemConfiguration.SynchronizationTime2 != null &&
                model.SystemConfiguration.SynchronizationTime3 != null);
            {
                var sysConfig = new DTO.SystemSync.SystemConfiguration
                {
                    SyncTime = dateTime.TimeOfDay,
                    SyncTime2 = dateTime2.TimeOfDay,
                    SyncTime3 = dateTime3.TimeOfDay
                };


                configService.UpdateSyncTime(sysConfig);
                configService.UpdateSyncTime2(sysConfig);
                configService.UpdateSyncTime3(sysConfig);

            }


            return RedirectToAction("Index");
        }



////My Private method

private SystemConfiguration GetSystemConfig()
        {
            var model = new SystemConfiguration();
            var config = configService.GetSyncTime();
                         configService.GetSyncTime2();
                         configService.GetSyncTime3();

            if (config == null) return model;
            var ts = config.SyncTime;
            if (ts != null)
            {
                model.SynchronizationTime = ts.ToString();
            }

            var ts2 = config.SyncTime2;
            if (ts2 != null)
            {
                model.SynchronizationTime2 = ts2.ToString();
            }

            var ts3 = config.SyncTime3;
            if (ts3 != null)
            {
                model.SynchronizationTime3 = ts3.ToString();
            }
            return model;
============================================================================
/// My configuration command


namespace --.--.Commands
{
    public class ConfigurationCommand : CommandBase, IConfigurationCommand
    {
        static ConfigurationCommand()
        {
            ConfigureAutoMapper();
        }

        private static void ConfigureAutoMapper()
        {
             Mapper.CreateMap<SystemConfiguration, entity.SystemConfiguration>()
                .ForMember(dest => dest.SyncTime, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime2, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime3, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>());
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();

            //if this is the first time, then we need to insert
            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime = mapped.SyncTime
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime = mapped.SyncTime;
            }
            SaveChanges();
        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime2 = mapped.SyncTime2
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime2 = mapped.SyncTime2;
            }
            SaveChanges();
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime3 = mapped.SyncTime3
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime3 = mapped.SyncTime3;
            }
            SaveChanges();
        }
    }
}



=========================================================================================================
// My configuration service


namespace --.--.--.SystemSync
{
    public class ConfigurationService : IConfigurationService
    {
        private IConfigurationQuery query;
        private IConfigurationCommand command;

        public ConfigurationService(IConfigurationQuery query,IConfigurationCommand command)
        {
            this.query = query;
            this.command = command;
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime(timeOfDay);

        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime2(timeOfDay);
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime3(timeOfDay);
        }


        public SystemConfiguration GetSyncTime()
        {
            return query.GetSyncTime();
        }

        public SystemConfiguration GetSyncTime2()
        {
            return query.GetSyncTime2();
        }

        public SystemConfiguration GetSyncTime3()
        {
            return query.GetSyncTime3();
        }


        public List<PageResource> GetPages()
        {
            return query.GetPages().ToList();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

你对固定时间做了评论,你在找这样的东西吗?

   CREATE TABLE [dbo].[Zamen](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [time1] [time](3) NOT NULL,
    [time2] [time](3) NOT NULL,
    [Content] [varchar](100) NULL,
 CONSTRAINT [PK_Zamen] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
))

GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time1]  DEFAULT (getdate()) FOR [time1]
GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time2]  DEFAULT (getdate()) FOR [time2]
GO

这些alter table语句允许自动插入时间。所以当你这样做时:

INSERT INTO Zamen (Content) VALUES ('demo')

将当前时间放入值中。

*看到你添加的代码后,输入一些: 在 UpdateTime 操作方法中,一个突出的问题是您调用UpdateTimeSync三次,但每次都传递三个变量。我建议重构你的更新方法 - 而不是三种更新方法,对所有时间变量使用一种更新方法。