/// <summary>
        /// 初始化系统分类
        /// </summary>
        /// <param name="context"></param>
        public static void SeedSystemCategory(ApplicationDbContext context)
        {
            if (context.DT_SystemCategory.Any())
            {
                return;
            }
            context.DT_SystemCategory.AddRange(
                new M_SystemCategory
                {
                    Name = "优先级别",
                    PID = 0,
                    Order = 1
                },
                new M_SystemCategory
                {
                    Name = "计量单位",
                    PID = 0,
                    Order = 2
                },
                new M_SystemCategory
                {
                    Name = "失石率",
                    PID = 0,
                    Order = 3
                },
                new M_SystemCategory
                {
                    Name = "机械类型",
                    PID = 0,
                    Order = 4
                },             );
            context.SaveChanges();
        }
我用这个生成数据库数据,但结果在数据库生成时,顺序是随机的,有什么办法让它顺序生成吗?

解决方案 »

  1.   

    你说的数据库是什么数据库?你说”顺序是随机的“,这里的顺序是什么?是你观察到顺序?还是查询倒的顺序?一般来说,关系数据库(包括mssql,oracle,mysql等等)的数学基础是居于集合理论。
    而集合是没有顺序概念。数据的插入顺序,删除顺序对关系数据库可以是无关紧要的。
    select * from SystemCategory 返回记录顺序是没有定义,我们也不能依赖于它的表现。关系数据库查询,只有在有order by条款下,顺序才有意义,比如
    select * from SystemCategory order by [Order] 就有明确的顺序意义。
      

  2.   

    如果你希望 自动排序, SortList<T> 如果数据库那就 SELECT * FROM TB ORDER BY 字段 ASC 或是 DESC 
      

  3.   

     context.DT_SystemCategory.AddRange(
                    new M_SystemCategory
                    {
                        Name = "优先级别",
                        PID = 0,
                        Order = 1
                    },
                    new M_SystemCategory
                    {
                        Name = "计量单位",
                        PID = 0,
                        Order = 2
                    },
                    new M_SystemCategory
                    {
                        Name = "失石率",
                        PID = 0,
                        Order = 3
                    },
                    new M_SystemCategory
                    {
                        Name = "机械类型",
                        PID = 0,
                        Order = 4
                    },
     
                 );
    这个方法,能不能改成类似 context.M_SystemCategory.add( new M_SystemCategory
                    {
                        Name = "优先级别",
                        PID = 0,
                        Order = 1
                    })
    context.M_SystemCategory.add( new M_SystemCategory
                    {
                      Name = "计量单位",
                        PID = 0,
                        Order = 2
                    })
    ......到最后再savechange
    如果还是不行,每add一次,就save一次
      

  4.   

    把数据丢到 LIst<T>  然后调用上下文 AddRange(List)
    不就行了,有时候有些东西不能省,省掉了BUG 就来了
      

  5.   


    不支持.add方法,直接报错
      

  6.   

    这样试试
     context.DT_SystemCategory.AddRange(
                    new M_SystemCategory
                    {
                        Name = "优先级别",
                        PID = 0,
                        Order = 1
                    })context.DT_SystemCategory.AddRange( new M_SystemCategory
                    {
                        Name = "计量单位",
                        PID = 0,
                        Order = 2
                    })
     context.SaveChanges();