I am completely new to EF Code First approach and I am facing issues regarding Migration result for one for my tables Histories.
I would like the Histories table to have a User foreign key (0..* relation: A user can have none or multiple histories):
public partial class User { public User() { this.Histories = new HashSet<History>(); } public System.Guid Id { get; set; } [InverseProperty("User")] public virtual ICollection<History> Histories { get; set; } } public partial class History { public History() { } public System.Guid Id { get; set; } public System.Guid UserId { get; set; } [ForeignKey("UserId")] public virtual User User { get; set; } } However this migration leads to an extra foreign key User_Id and whatever I try I just can't prevent it from being created.
CreateTable( "dbo.Histories", c => new { Id = c.Guid(nullable: false), UserId = c.Guid(), User_Id = c.Guid(), }) .PrimaryKey(t => t.Id) .ForeignKey("dbo.Users", t => t.UserId) .ForeignKey("dbo.Users", t => t.User_Id) .Index(t => t.UserId) .Index(t => t.User_Id); I'm using Data Annotations but also tried with the OnModelCreating method, unsuccessfully.
I read lots of posts, notably this one (http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx) but couldn't find a working solution.
Would someone please explain me what I am doing wrong and what I have missed ?
Thanks a lot.
InversePropertyadds one more extra foreign key (there is UserId, User_Id and User_Id1). @Ivan: I already removed the database, deleted theMigrationsfolder and didAdd-Migration init. Both classes actually contain much more properties, is there any chance that one of them could be involved ?