Adelino Safeca

Adelino Safeca

  • NA
  • 6
  • 600

Why Migrations are not using EntityTypeConfiguration?

Aug 21 2018 10:33 AM
I have a project in Asp.Net MVC using Migrations.
 
The problem is that when I run the migration it creates additional properties which I didnt declared in my models.
 
Example for 'User' model:
 
public int Id { get; set; }
public string Nome { get; set; }
public int IdPosition { get; set; }
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public virtual Position position { get; set; }
 
When I run the migration it creates an additional property called position_Id and maps it as the  foreign key. Like Below:
 
CreateTable(
"dbo.User",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 100, unicode: false),
IdPosition = c.Int(nullable: false),
Username = c.String(nullable: false, maxLength: 100, unicode: false),
Password = c.String(nullable: false, maxLength: 100, unicode: false),
position_Id = c.Int(), //Does not exist on model neither on EntityTypeConfig file

})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Position", t => t.position_Id)
.Index(t => t.position_Id); //does not exist on model neither on EntityTypeConfig file.
 
 
But I have already declared IdPosition as the Foreign Key on EntityTypeConfiguration file.cs, Like this:
 
protected override void ConfigForeignKey()
{
HasRequired(k => k.position)
.WithMany()
.HasForeignKey(fk => fk.IdPosition)
.WillCascadeOnDelete(false);
}
 
 
I Really need to know what I'm doing wrong. Thanks on advance

Answers (1)