I have the following code:
modelBuilder.Entity<User>().HasMany(x => x.Items).WithRequired();
The model looks like
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Item
{
public long Id { get; set; }
public string Title { get; set; }
public virtual User User { get; set; }
}
When EF generates the database for me I end up with two foreign keys in the Items table (User_Id and User_Id1) instead of just one. How would I configure this so only one key is created for me?
Just for testing purposes I removed the User from the Item class and in that case the configuration works just fine and only one key is created.
Not sure if this is just a typo in your question, but I’m pretty sure your modelBuilder statement should be
modelBuilder.Entity<User>().HasMany(x => x.Items).WithRequired(i => i.User);Not sure if that’s your issue or not, but I’d certainly start there.
You probably also want to add the Long ID FK in your Item class