/// <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();
}
我用这个生成数据库数据,但结果在数据库生成时,顺序是随机的,有什么办法让它顺序生成吗?
而集合是没有顺序概念。数据的插入顺序,删除顺序对关系数据库可以是无关紧要的。
select * from SystemCategory 返回记录顺序是没有定义,我们也不能依赖于它的表现。关系数据库查询,只有在有order by条款下,顺序才有意义,比如
select * from SystemCategory order by [Order] 就有明确的顺序意义。
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一次
不就行了,有时候有些东西不能省,省掉了BUG 就来了
不支持.add方法,直接报错
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();