【发布时间】:2016-04-14 22:00:55
【问题描述】:
我在本地计算机上遇到了 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);
});
【问题讨论】:
标签: c# asp.net sql-server entity-framework asp.net-core