将以实体框架代码为先的SQL Server数据库字母列顺序更改为顺序 [英] Change SQL Server database alphabetic column ordering in Entity Framework code-first to sequential

查看:68
本文介绍了将以实体框架代码为先的SQL Server数据库字母列顺序更改为顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要先关闭代码中的字母顺序。

I need to turn off alphabetic ordering in code first.

这是我的课程简化了

public class Person
{
    [Key,Column("PersonId")]
    public int Id { get; set; }
    [MaxLength(50)]
    public string PersonName{ get; set; }
    public DateTime? JoinDate{ get; set; }
    public int? Gender{ get; set; }
}

,当我运行命令生成数据库时

and when I run the commands to generate the database

dnx ef migrations add InitialMigration
dnx ef  database update

当我在SQL Server 2012中的设计模式下查看数据库时,除主键之外的数据库列均按字母顺序生成。

The database columns apart from the primary key generates in alphabetic order when I view it in design mode in SQL Server 2012.

我如何强制它按照类中出现的顺序创建列。

How do I force it to create the columns in sequential order as it appears in the class.

我在github上看了一下,只能找到问题,没有说明如何将其关闭。

I had a look on github and could only find this issue which doesn't explain how to turn it off.

推荐答案

在EF7迁移中,对此行为没有一流的支持。您可以通过显式指定迁移操作的SQL来解决此问题。

There is no first-class support for this behavior in EF7 migrations. You can workaround this by explicitly specifying SQL your migration operations.

这意味着您无需在迁移中使用 CreateTable方法,而必须显式编写SQL。 / p>

That means instead of using the "CreateTable" method in your migrations, you need to explicitly write the SQL.

migrationBuilder.Sql("CREATE TABLE Person ...");

这篇关于将以实体框架代码为先的SQL Server数据库字母列顺序更改为顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆