예제 #1
0
        /// <summary>
        /// Initialises the Entity Framework model
        /// </summary>
        /// <param name="modelBuilder">EF model builder</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ConfigureConventions();
            ConfigureManyToMany(modelBuilder);

            // Special cases
            modelBuilder.Entity <ProjectTechnologyModel>().ToTable("project_techs");
            modelBuilder.Entity <PostModel>().ToTable("blog_posts");
            modelBuilder.Entity <PostModel>().Property(x => x.MainCategoryId).HasColumnName("maincategory_id");
            modelBuilder.Entity <CategoryModel>().ToTable("blog_categories");
            modelBuilder.Entity <CategoryModel>().Property(x => x.ParentId).HasColumnName("parent_category_id");
            modelBuilder.Entity <TagModel>().ToTable("blog_tags");
            modelBuilder.Entity <TagModel>().Property(x => x.ParentId).HasColumnName("parent_tag_id");
            modelBuilder.Entity <DisqusCommentModel>()
            .ToTable("disqus_comments")
            .Ignore(x => x.Children);

            modelBuilder.Entity <PostModel>()
            .Property(x => x.RawContent)
            .HasColumnName("content");

            // Backwards compatibility with old DB - Dates as UNIX times
            modelBuilder.Entity <PostModel>()
            .Property(x => x.Date)
            .HasConversion(
                x => x.ToUnix(),
                x => DateExtensions.FromUnix(x)
                );

            modelBuilder.Entity <PostModel>()
            .Property(x => x.ShareCounts)
            .HasColumnName("share_counts")
            .HasConversion(
                x => JsonConvert.SerializeObject(x),
                x => string.IsNullOrEmpty(x) ? null : JsonConvert.DeserializeObject <IDictionary <string, int> >(x)
                );

            modelBuilder.Entity <ProjectModel>()
            .Property(x => x.ProjectType)
            .HasColumnName("type")
            .HasConversion(
                x => x.ToString(),
                x => x.FirstUpper().ParseEnum <ProjectType>()
                );

            modelBuilder.Entity <ProjectModel>()
            .Property(x => x.Technologies)
            .HasConversion(
                x => string.Join(",", x),
                x => x.Split(',')
                );
        }
 public void FromUnix()
 {
     Assert.AreEqual(new DateTime(year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Utc).ToLocalTime(), DateExtensions.FromUnix(0));
     Assert.AreEqual(new DateTime(year: 2012, month: 12, day: 26, hour: 7, minute: 40, second: 58, kind: DateTimeKind.Utc).ToLocalTime(), DateExtensions.FromUnix(1356507658));
 }