简体   繁体   中英

How to delete column from db without having migrations enabled?

I have a .net core 2.1 application in which I have a small SqlServer 2016 database that contains only one table ie Product.

Now I have a DbContext like:

    public class DataContext : DbContext
    {
      public DataContext(DbContextOptions<DataContext> options) : base(options)
      {

      }

      public virtual DbSet<Product> Product { get; set; }
    }

and the product model:

    [DataContract]
    public class Product
    {
        [DataMember]
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [DataMember]
        [MaxLength(100)]
        public string Name { get; set; }
        [DataMember]
        public DateTime CreatedDate { get; set; }
        [DataMember]
        public string CreatedBy { get; set; }
        [DataMember]
        public DateTime UpdatedDate { get; set; }
        [DataMember]
        public string UpdatedBy { get; set; }
    }

and in Program.cs file I am calling this like:

var services = scope.ServiceProvider;
var context = services.GetRequiredService<DataContext>();
context.Database.EnsureCreated();

Now the issue is I don't have migrations enabled in my project and I want to remove 2 columns ie CreatedBy and UpdatedBy from by db. But I have to delete the table so the changes can be reflected. Is there anyway to do that without deleting the table? Like what we do when we have migrations enabled. Add-Migration and Update-Database . Can I do this with context.Database>EnsureCreated() ?

I think it's not possible without having migrations enabled. So I have enabled migrations in my application and changed context.Database.EnsureCreated() to context.Database.Migrate() and now everything is working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM