I am experiencing some strange issues with EF7-codefirst migrations and SQL Server on my local machine. When I run the following in command-prompt:
dnx ef migrations add InitialDatabse
It is creating the columns in alphabetical order. From what I remember the default is usually to create the columns in the same order they are in the class.
Here is my concern:
Why is it creating my columns in alphabetical order?
Here are my models and 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; }
}
Here is my InitialDatabase.cs file after adding migrations to the project:
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);
});