首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架6外键

实体框架6外键
EN

Stack Overflow用户
提问于 2014-10-23 03:39:43
回答 1查看 588关注 0票数 0

我正在用MVC5用EF6编写一个互联网应用程序,并且有一个关于外键名称的问题。

我有一个名为MapLocationList的模型,它包含以下两个字段:

代码语言:javascript
复制
public int mapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery mapLocationListGallery { get; set; }

当EF创建表时,有以下两列:

  • mapLocationListGalleryId
  • MapLocationListGallery_Id

有人能解释一下为什么MapLocationListGallery外键有两列吗?

提前感谢

编辑

我已将名称更改为大写M,但附加列仍然存在。

这是我的模型:

代码语言:javascript
复制
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函数中还有以下内容:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2014-10-23 07:38:22

我也使用这种方法,而且我没有经历这种行为。可能需要将属性重命名为CamelCase (注意大写M):

代码语言:javascript
复制
public int MapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery MapLocationListGallery { get; set; }

如果这没有帮助的话,请看一下ForeignKeyAttribute 这里这里

编辑

我不熟悉fluent api,但我认为您可以尝试使用以下方法显式设置外键:

代码语言:javascript
复制
modelBuilder.Entity<MapLocationList>()
    .HasRequired(c => c.mapLocationListGallery)
    .WithMany()
    .HasForeignKey(x => x.mapLocationListGalleryId)
    .WillCascadeOnDelete(false);

有关更多信息,请参见这篇文章的主题:“配置非常规外键名称”。虽然这很奇怪,但这是必要的,因为您的代码似乎符合code约定(带有大写字母M,即类名)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26520930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档