The following entity configuration it creating a double foreign key for one of the navigation properties:
public User : IdentityUser
{
//public string Id { get; set; } //on base class
public virtual ICollection<Earning> Earnings { get; set; }
}
public class Earning
{
public int EarningId { get; set; }
public virtual User User { get; set; }
public string UserId { get; set; }
public virtual User Sender { get; set; }
public string SenderId { get; set; }
}
public class EarningConfiguraiton : EntityTypeConfiguration<Earning>
{
public EarningConfiguraiton()
{
this.HasRequired(e => e.User)
.WithMany(u => u.Earnings)
.HasForeignKey(e => e.UserId);
this.HasRequired(e => e.Sender)
.WithMany()
.HasForeignKey(e => e.SenderId)
.WillCascadeOnDelete(false);
}
}
However it generates 3 foreign keys for 2 navigation properties. "User_Id" is seemingly redundant. Should I remove , if so how?
CreateTable(
"dbo.Earnings",
c => new
{
EarningId = c.Int(nullable: false, identity: true),
UserId = c.String(maxLength: 128),
SenderId = c.String(maxLength: 128),
User_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.EarningId)
.ForeignKey("dbo.AspNetUsers", t => t.SenderId)
.ForeignKey("dbo.AspNetUsers", t => t.UserId)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.UserId)
.Index(t => t.SenderId)
.Index(t => t.User_Id);
Further more context.Users.Include(u => u.Earnings)
only works with User_Id
column populated, rather than UserId.
I even get this migration generated on a brand new database