Arun Prasad E S
Arun Prasad E S

Reputation: 10115

EF core , Code First Table rename not detected for migration definition, Scafolder is empty

I get empty migration builder when I change a class name in EF core.

In older EFs , it usually auto generate the code for renaming tables.

But not working in EF core

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; }
}

now changing EventComment to CommentMaster. The migration is empty.

I use fluent API for configuring

        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);

Upvotes: 1

Views: 403

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205589

This is because of the EF Core default table mapping convention (highlights are mine):

By convention, each entity will be setup to map to a table with the same name as the DbSet<TEntity> property that exposes the entity on the derived context. If no DbSet<TEntity> is included for the given entity, the class name is used.

I guess this is different from the previous EF. The essential part is that although you renamed the entity class, if you keep the old DbSet property name, the table name will not change.

Upvotes: 2

Related Questions