我正在使用EF6编写MVC5互联网应用程序,并对外键名称有疑问。
我有一个名为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);
答案 0 :(得分:1)
我也使用这种方法,我没有遇到这种行为。您可能需要将属性重命名为CamelCase(注意大写M):
public int MapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery MapLocationListGallery { get; set; }
如果这没有帮助,请查看ForeignKeyAttribute
here和here。
修改强>
我不熟悉流畅的api,但我认为您可以尝试使用以下内容明确设置外键:
modelBuilder.Entity<MapLocationList>()
.HasRequired(c => c.mapLocationListGallery)
.WithMany()
.HasForeignKey(x => x.mapLocationListGalleryId)
.WillCascadeOnDelete(false);
有关详细信息,请参阅this article,主题:&#34;配置非常规外键名称&#34;。虽然很奇怪,但这是必要的,因为您的代码似乎符合Code First约定(使用大写字母M,即类名)。