【问题标题】:EF core , Code First Table rename not detected for migration definition, Scafolder is emptyEF 核心,未检测到迁移定义的 Code First 表重命名,Scafolder 为空
【发布时间】:2017-05-16 09:07:35
【问题描述】:

当我在 EF 核心中更改类名时,我得到空的迁移构建器。

在较旧的 EF 中,它通常会自动生成重命名表的代码。

但不能在 EF 核心中工作

public class EventComment : Comment
{
    [Key]
    public int CommentID { get; set; }

    public int? ParentID { get; set; }
    public virtual EventComment Parent { get; set; }

    public int EventID { get; set; }
    public virtual EventMaster EventMaster { get; set; }

}

public class Comment
{
    public string CommentTitle { get; set; }

    public string CommentDetails { get; set; }

    public int UpVoteCount { get; set; }
    public int DownVoteCount { get; set; }
    public int CommentEmotion { get; set; }

    public string CommentedByID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

现在将 EventComment 更改为 CommentMaster。迁移为空。

我使用 fluent API 进行配置

        builder.Entity<EventComment>()
            .HasOne(e => e.EventMaster)
            .WithMany()
            .HasForeignKey(m => m.EventID);

        builder.Entity<EventComment>()
            .HasOne(e => e.ApplicationUser)
            .WithMany()
            .HasForeignKey(m => m.CommentedByID);

        builder.Entity<EventComment>()
            .HasOne(e => e.Parent)
            .WithMany()
            .HasForeignKey(x => x.ParentID);

【问题讨论】:

    标签: ef-code-first migration entity-framework-core


    【解决方案1】:

    这是因为 EF Core 默认表映射convention(重点是我的):

    按照惯例,每个实体都将设置为映射到具有与 DbSet 属性同名 的表,该属性在派生上下文中公开实体。如果给定实体没有包含 DbSet,则使用类名。

    我想这与之前的 EF 不同。重要的是,虽然你重命名了实体类,但如果保留旧的DbSet属性名,表名不会改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多