gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:50:41 24 4
gpt4 key购买 nike

我在本地计算机上遇到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com