我在本地计算机上遇到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);
});
答案 0 :(得分:2)
很难确保可预测的订单,因此目前尚未实施按物业订单排序。 请参阅此处的讨论https://github.com/aspnet/EntityFramework/issues/2272
答案 1 :(得分:0)
由于.NET Core面向非Windows平台,因此自动化流程无法保证以编码顺序为引擎提供模型的属性。当我第一次发现这个问题时,我记得它与Reflection有关,但不是细节。
无论如何,要解决此问题,请使用数据注释强制列订单。请注意,您使用的实际数字不需要以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; }
}
有关代码优先的数据注释的更多信息,请查看官方.NET Core文档:
https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx