Reputation: 57
When I make a migration file in Azure .Net Backend, it automatically generates the five required fields(Id,CreatedAt, UpdatedAt, Version, Deleted) with my customer fields. It defines Id as the primary key.
public override void Up()
{
CreateTable(
"dbo.People",
c => new
{
Id = c.String(nullable: false, maxLength: 128,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Id")
},
}),
PersonType = c.String(nullable: false, maxLength: 2),
FirstName = c.String(nullable: false, maxLength: 50),
MiddleName = c.String(maxLength: 50),
LastName = c.String(nullable: false, maxLength: 50),
Gender = c.String(nullable: false, maxLength: 1),
BirthDate = c.DateTime(nullable: false, storeType: "date"),
Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion",
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Version")
},
}),
CreatedAt = c.DateTimeOffset(nullable: false, precision: 7,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "CreatedAt")
},
}),
UpdatedAt = c.DateTimeOffset(precision: 7,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "UpdatedAt")
},
}),
Deleted = c.Boolean(nullable: false,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Deleted")
},
}),
})
.PrimaryKey(t => t.Id)
.Index(t => t.CreatedAt, clustered: true);
}
I want to change this primary key name "Id" to "PersonId", so I add a primary key property like below
public class Person : EntityData
{
public string PersonId;
....
}
But it did not work. "Id" was not replaced by "PersonId" and just added as a customer field in the migration file. So, Added the [Key] attribute but it made an error and I got the below message.
"Unable to determine composite primary key ordering for type . Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys."
How I can change the primary key name?
Upvotes: 0
Views: 433
Reputation: 134
You can use the HasKey method to set the property to be a primary Key.
For example,
modelBuilder.Entity<Person>().HasKey(t => t.PersonId);
It will set the PersonId as the primary key.
Good Luck.
Upvotes: 1