c# - ASP.NET 5 EF7 代码优先迁移默认按字母顺序创建列

标签 c# asp.net sql-server entity-framework asp.net-core

我在本地计算机上遇到 EF7-codefirst 迁移和 SQL Server 的一些奇怪问题。当我在命令提示符下运行以下命令时:

dnx ef migrations add InitialDatabse

它按字母顺序创建列。据我所知,默认情况下通常是按照它们在类中的相同顺序创建列。

这是我的担忧:
为什么它按字母顺序创建我的列?

这是我的模型和 Dbcontext:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public string CreatedBy { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }

    // Navigation properties
    public ICollection<Contact> Contacts { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
    public string Email { get; set; }
    public DateTime CreatedDate { get; set; } = DateTime.Now;
    public string CreatedBy { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext ()
    {
        Database.EnsureCreated();
    }

    public DbSet<Customer> Customers { get; set; }
    public DbSet<Contact> Contacts { get; set; }
}

这是我向项目添加迁移后的 InitialDatabase.cs 文件:

    migrationBuilder.CreateTable(
        name: "Customer",
        columns: table => new
        {
            Id = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            CreatedBy = table.Column<string>(nullable: true),
            CreatedDate = table.Column<DateTime>(nullable: false),
            ModifiedBy = table.Column<string>(nullable: true),
            ModifiedDate = table.Column<DateTime>(nullable: true),
            Name = table.Column<string>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Customer", x => x.Id);
        });
    migrationBuilder.CreateTable(
        name: "Contact",
        columns: table => new
        {
            Id = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            CreatedBy = table.Column<string>(nullable: true),
            CreatedDate = table.Column<DateTime>(nullable: false),
            CustomerId = table.Column<int>(nullable: true),
            Email = table.Column<string>(nullable: true),
            FirstName = table.Column<string>(nullable: true),
            LastName = table.Column<string>(nullable: true),
            ModifiedBy = table.Column<string>(nullable: true),
            ModifiedDate = table.Column<DateTime>(nullable: true),
            Title = table.Column<string>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Contact", x => x.Id);
            table.ForeignKey(
                name: "FK_Contact_Customer_CustomerId",
                column: x => x.CustomerId,
                principalTable: "Customer",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        });

最佳答案

很难确保可预测的顺序,因此目前未实现按属性排序。 请参阅此处的讨论 https://github.com/aspnet/EntityFramework/issues/2272

关于c# - ASP.NET 5 EF7 代码优先迁移默认按字母顺序创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707225/

相关文章:

c# - 在 C# 中将枚举列表转换为标志

asp.net - 在与 ASP.Net 应用程序相同的端口上启动 .Net Web 服务

sql - 如何避免在嵌套存储过程的嵌套事务中使用重复的保存点名称?

c# - 现在 VS2015 已经出来了,支持调试 Roslyn 的方式是什么?

c# - Parallel.For 没有正确处理锁

c# - 领域驱动设计-父子关系模式-规范模式

javascript - ASPxGridView 停止回调

c# - 获取在 RadGrid 中触发 ItemCommand 的控件的 ID

sql-server - 在 T-SQL 中获取过去 24 小时结果的最佳方法是什么?

sql-server - 对对象 'xxxxxxx'、数据库 'zzzzzzz'、架构 'dbo' 的 EXECUTE 权限被拒绝