Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 默认情况下,ASP.NET 5 EF7 code first迁移将按字母顺序创建列_C#_Asp.net_Sql Server_Entity Framework_Asp.net Core - Fatal编程技术网

C# 默认情况下,ASP.NET 5 EF7 code first迁移将按字母顺序创建列

C# 默认情况下,ASP.NET 5 EF7 code first迁移将按字母顺序创建列,c#,asp.net,sql-server,entity-framework,asp.net-core,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 str

我在本地计算机上的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; }
}
公共类客户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public DateTime CreatedDate{get;set;}=DateTime.Now;
通过{get;set;}创建的公共字符串
公共日期时间?修改日期{get;set;}
公共字符串由{get;set;}修改
//导航属性
公共ICollection联系人{get;set;}
}
公共类联系人
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串标题{get;set;}
公共字符串电子邮件{get;set;}
public DateTime CreatedDate{get;set;}=DateTime.Now;
通过{get;set;}创建的公共字符串
公共日期时间?修改日期{get;set;}
公共字符串由{get;set;}修改
}
公共类ApplicationDbContext:DbContext
{
公共应用程序上下文()
{
数据库。请重新创建();
}
公共数据库集客户{get;set;}
公共数据库集联系人{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);
        });
migrationBuilder.CreateTable(
名称:“客户”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
CreatedBy=table.Column(可空:true),
CreatedDate=table.Column(可空:false),
ModifiedBy=table.Column(可空:true),
ModifiedDate=table.Column(可空:true),
Name=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_客户”,x=>x.Id);
});
migrationBuilder.CreateTable(
姓名:“联系人”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
CreatedBy=table.Column(可空:true),
CreatedDate=table.Column(可空:false),
CustomerId=table.Column(可空:true),
Email=table.Column(可空:true),
FirstName=table.Column(可空:true),
LastName=table.Column(可空:true),
ModifiedBy=table.Column(可空:true),
ModifiedDate=table.Column(可空:true),
Title=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_联系人”,x=>x.Id);
表1.外键(
名称:“FK\U联系人\U客户\U客户ID”,
列:x=>x.CustomerId,
原则性:“客户”,
主栏:“Id”,
onDelete:referentialiction.Restrict);
});

很难确保可预测的顺序,因此目前未实现按属性顺序排序。
请参阅此处的讨论

因为.NET Core针对的是非Windows平台,所以自动化流程无法保证模型的属性以编码顺序提供给引擎。当我第一次发现这个问题时,我记得它与反思有关,但与细节无关

无论如何,要解决此问题,请使用数据注释强制执行列顺序。请注意,您使用的实际数字不需要以1为间隔,您可以使用10、20、30等值,因此如果需要移动/添加列,您可以使用15等值,而无需重新调整模型中的每个属性

我还没有成功创建改变列顺序的迁移;我只是从头开始删除/重建我的表

修改您的示例:

public class Customer
{
    [Column(Order = 10)]
    public int Id { get; set; }

    [Column(Order = 20)]
    public string Name { get; set; }

    [Column(Order = 30)]
    public DateTime CreatedDate { get; set; } = DateTime.Now;

    [Column(Order = 40)]
    public string CreatedBy { get; set; }

    [Column(Order = 50)]
    public DateTime? ModifiedDate { get; set; }

    [Column(Order = 60)]
    public string ModifiedBy { get; set; }

    // Navigation properties
    public ICollection<Contact> Contacts { get; set; }
} 
公共类客户
{
[第列(顺序=10)]
公共int Id{get;set;}
[第列(顺序=20)]
公共字符串名称{get;set;}
[第列(顺序=30)]
public DateTime CreatedDate{get;set;}=DateTime.Now;
[第列(顺序=40)]
通过{get;set;}创建的公共字符串
[第列(顺序=50)]
公共日期时间?修改日期{get;set;}
[第列(顺序=60)]
公共字符串由{get;set;}修改
//导航属性
公共ICollection联系人{get;set;}
} 
有关代码中数据注释的更多信息,请首先查看官方.NET核心文档:


在my DbContext中是否有用于指定顺序的设置或任何内容?您可以使用列属性来指定顺序。[列(“名称”,顺序=1)]同一问题: