【问题标题】:ASP.NET MVC5 add 2 Sync Times to the ApplicationASP.NET MVC5 向应用程序添加 2 个同步时间
【发布时间】:2017-07-18 23:35:07
【问题描述】:

我将 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();
        }

    }
}

【问题讨论】:

  • 如果这会让事情变得更容易,有没有办法在数据库中固定这些时间?
  • 如果您压缩标题以建议您尝试纠正的行为,您可能会获得更多浏览量。
  • 我很抱歉,我想我试图尽可能地描述性。

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


【解决方案1】:

你发表了关于固定时间的评论,你在寻找这样的东西吗?

   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 三次,但每次都将三个变量都传递给它。我建议重构你的更新方法——而不是三种更新方法,对所有时间变量使用一种更新方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2014-06-29
    相关资源
    最近更新 更多