Simon
Simon

Reputation: 8347

Entity Framework 6 foreign key

I am coding an MVC5 internet application with EF6, and have a question in regards to a foreign key name.

I have a model called MapLocationList that has these two fields:

public int mapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery mapLocationListGallery { get; set; }

When EF creates the table, there is both the following columns:

Can someone please explain why there are two columns for the MapLocationListGallery foreign key?

Thanks in advance

EDIT

I have changed the name to use an uppercase M, yet the additional column is still there.

Here is my model:

public class MapLocationList : IMapLocationItemWithAssets
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string name { get; set; }
    public bool enabled { get; set; }
    [ScaffoldColumn(false)]
    public string mapLocationItemType { get; set; }
    [ScaffoldColumn(false)]
    public string userName { get; set; }
    [ScaffoldColumn(false)]
    public DateTime creationDate { get; set; }
    [ScaffoldColumn(false)]
    public DateTime lastUpdate { get; set; }
    public string thumbnailDisplayText { get; set; }
    public bool parentIsMapLocation { get; set; }
    public int thumbnailAssetId { get; set; }
    public virtual Asset thumbnailAsset { get; set; }
    public int mapLocationId { get; set; }
    public virtual MapLocation mapLocation { get; set; }
    public int mapLocationListGalleryId { get; set; }
    public virtual MapLocationListGallery mapLocationListGallery { get; set; }

    public virtual ICollection<MapLocationListItem> listItems { get; set; }

    public MapLocationList()
    {
        creationDate = DateTime.Now;
        lastUpdate = DateTime.Now;
        listItems = new List<MapLocationListItem>();
    }
}

I also have the following in the OnModelCreating function:

modelBuilder.Entity<MapLocationListGallery>()
    .HasRequired(c => c.thumbnailAsset)
    .WithMany()
    .WillCascadeOnDelete(false);

modelBuilder.Entity<MapLocationList>()
    .HasRequired(c => c.thumbnailAsset)
    .WithMany()
    .WillCascadeOnDelete(false);

modelBuilder.Entity<MapLocationList>()
    .HasRequired(c => c.mapLocationListGallery)
    .WithMany()
    .WillCascadeOnDelete(false);

modelBuilder.Entity<MapLocationListItem>()
    .HasRequired(c => c.thumbnailAsset)
    .WithMany()
    .WillCascadeOnDelete(false);

Upvotes: 0

Views: 598

Answers (1)

Marthijn
Marthijn

Reputation: 3392

I use this approach as well and I do not experience this behavior. Probably you need to rename your properties to CamelCase (note the capital M):

public int MapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery MapLocationListGallery { get; set; }

If that doesn't help take a look at the ForeignKeyAttribute here and here.

Edit

I'm not familiar with the fluent api, but I think you could try to set the foreign key explicitly using something like:

modelBuilder.Entity<MapLocationList>()
    .HasRequired(c => c.mapLocationListGallery)
    .WithMany()
    .HasForeignKey(x => x.mapLocationListGalleryId)
    .WillCascadeOnDelete(false);

For more info see this article, topic: "Configuring Unconventional Foreign Key Names". Although it's strange this is necessary because your code seems to comply with the Code First convention (with capital M, i.e. the class name).

Upvotes: 1

Related Questions