1

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.

3
  • Try remove [InverseProperty("User")]. What is in the other partial class? Commented May 27, 2016 at 10:05
  • The model and configuration look ok. Try regenerating the migration. Commented May 27, 2016 at 11:00
  • @tmg: Removing the InverseProperty adds one more extra foreign key (there is UserId, User_Id and User_Id1). @Ivan: I already removed the database, deleted the Migrations folder and did Add-Migration init. Both classes actually contain much more properties, is there any chance that one of them could be involved ? Commented May 27, 2016 at 11:10

2 Answers 2

1

I finally figured out what was going on. The InverseProperty is actually needed, otherwise much more extra foreign keys are created, but the issue was due to this property in User other partial class:

public List<History> History { get { return Histories.OrderByDescending(h => h.Clock).ThenBy(h => h.Type).ToList(); } } 

Adding the [NotMapped] attribute did the trick and prevented the extra foreign key from being added.
I did not know that properties without set would be considered as columns, but seems like that's the case and caused this problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use fluent API this way:

modelBuilder.Entity<History>().HasRequired(x=> x.User).WithMany(x=> x.Histories).HasForeignKey(x=> x.UserId); modelBuilder.Entity<User>().HasMany(x=> x.Histories).WithRequired(x=> x.User).HasForeignKey(x=> x.UserId); 

Property without setter has equal behavior as [NotMapped] attribute.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.