I am coding a MVC 5 internet application and I have a question in regards to a foreign key reference in a model of mine.
I have a model with the following attributes:
Code:
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; }
When EF 6 creates a table in the database, the following fields are created:
Code:
thumbnailAssetId
mapLocationId
mapLocationListGalleryId
MapLocationListGalleryId
Why are both `mapLocationListGalleryId` and `MapLocationListGalleryId` created?
This is causing a lot of problems when editing an object's `mapLocationListGallery` to be another `mapLocationListGallery` as only the `mapLocationListGalleryId` is changing, and not the `MapLocationListGalleryId`.
How can I modify or write some code that will only have the `mapLocationListGalleryId` field? I have tried to use the `[ForeignKey("mapLocationListGalleryId")]`, however, both `mapLocationListGalleryId` and `MapLocationListGalleryId` are still created for the table.
Here is my full model class code:
Code:
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; }
[Required]
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 have the following in my `OnModelCreating` context class:
Code:
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);
Thanks in advance