我正在用MVC5用EF6编写一个互联网应用程序,并且有一个关于外键名称的问题。
我有一个名为MapLocationList的模型,它包含以下两个字段:
public int mapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery mapLocationListGallery { get; set; }当EF创建表时,有以下两列:
有人能解释一下为什么MapLocationListGallery外键有两列吗?
提前感谢
编辑
我已将名称更改为大写M,但附加列仍然存在。
这是我的模型:
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>();
}
}在OnModelCreating函数中还有以下内容:
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);发布于 2014-10-23 07:38:22
我也使用这种方法,而且我没有经历这种行为。可能需要将属性重命名为CamelCase (注意大写M):
public int MapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery MapLocationListGallery { get; set; }如果这没有帮助的话,请看一下ForeignKeyAttribute 这里和这里。
编辑
我不熟悉fluent api,但我认为您可以尝试使用以下方法显式设置外键:
modelBuilder.Entity<MapLocationList>()
.HasRequired(c => c.mapLocationListGallery)
.WithMany()
.HasForeignKey(x => x.mapLocationListGalleryId)
.WillCascadeOnDelete(false);有关更多信息,请参见这篇文章的主题:“配置非常规外键名称”。虽然这很奇怪,但这是必要的,因为您的代码似乎符合code约定(带有大写字母M,即类名)。
https://stackoverflow.com/questions/26520930
复制相似问题