/*sql script:
         * CREATE TABLE [dbo].[ExtensionMappingType](
         *      [Id] [int] IDENTITY(1,1) NOT NULL,
         *      [Name] [nvarchar](100) NULL,
         *      [F_Char] [nvarchar](1) NULL,
         *      [F_Time] [time](7) NULL,
         * CONSTRAINT [PK_ExtensionMappingType] PRIMARY KEY CLUSTERED
         * (
         *      [Id] ASC
         * )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
         * ) ON [PRIMARY]
         */

        public static void RunDemo()
        {
            //step 1:
            /* 原生 Chloe 不支持 char 和 TimeSpan 类型映射,需要我们自己注册,注册映射类必须在程序启动时进行 */
            MappingTypeSystem.Register(typeof(char), DbType.StringFixedLength);
            MappingTypeSystem.Register(typeof(TimeSpan), DbType.Time);

            //step 2:
            /* 因为我们新增了 MappingType,所以需要对原生的 SqlConnection、SqlServerCommand、SqlServerDataReader、SqlServerParameter 包装处理,所以,我们需要自个儿实现 IDbConnectionFactory 工厂  */
            SqlServerDbConnectionFactory sqlServerDbConnectionFactory = new SqlServerDbConnectionFactory(DbHelper.ConnectionString);
            MsSqlContext context = new MsSqlContext(sqlServerDbConnectionFactory);

            /* 经过上述封装,我们就可以支持 char 和 TimeSpan 类型映射了 */

            ExtensionMappingType entity = new ExtensionMappingType();

            entity.Name   = "test";
            entity.F_Char = 'A';
            entity.F_Time = TimeSpan.FromHours(12);
            context.Insert(entity);
            Console.WriteLine(entity.Id);

            TimeSpan             ts    = TimeSpan.FromHours(12);
            ExtensionMappingType model = context.Query <ExtensionMappingType>().Where(a => a.F_Time == ts && a.Id == entity.Id).First();

            Console.WriteLine(model.Id == entity.Id);


            Console.WriteLine("Yeah!!");
            Console.ReadKey();
        }