I recently came across this strange problem with Entity Framework Code First.
My class looks like this
public class Status
{
[Key]
pu
I've seen this before. In my case (Using EF 6.1), it was because my Fluent API Mapping was set up like so:
// In my EntityTypeConfiguration<Status>
HasRequired(x => x.Member).WithMany().HasForeignKey(x => x.MemberID);
That code works perfectly fine, but it doesn't tell EF that my Member
class's Collection Navigational Property Status
ha been taken into account. So, while I explicitly handled the existence of a Member
Navigational Property in my Status
Class, I now left an orphaned related collection property. That orphaned property, being a collection, tells EF that my Status
class needs to have a Foreign Key to it. So it creates that on the Status
Class.
To fix it, I had to be 100% explicit.
HasRequired(x => x.Member).WithMany(x => x.Statuses).HasForeignKey(x => x.MemberID)
It could bee that your Statuses
Collection property in Member
needs an attribute telling it that it is already considered, and not to go auto-creating mappings. I don't know that attribute.
Your Member_MemberID
column is created because of the Member.Statuses
property. I can imagine that this is not what you want. Probably members and statuses should exist independent of each other, so you need a junction table.
I don't know if you already use the OnModelCreating
override of the DbContext, but that's the place to change the mapping between Member and Status:
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Member>().HasMany(m => m.Statuses).WithMany();
}
This will create a table MemberStatuses table with the two Id columns as foreign keys. This is a way to model a many-to-many relationship without a navigation property on the "other" side of the association. (I don't think you want a Members
property in Status
).