I cannot update entity

179 views Asked by At

Category Table ;

    public int CategoryId{ get; set; }
    public string Name{ get; set; }
    public List<ProductCategory> ProductCategories { get; set; }

I add new field to the table;

    public int CategoryId{ get; set; }
    public string Name{ get; set; }
    public List<ProductCategory> ProductCategories { get; set; }
    public string Url{ get; set; }

I get the following error when updating entity; Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'

 eShopContext;
            public DbSet<Product> Products{ get; set; }
            public DbSet<Category> Categories{ get; set; }
            public object Configuration { get; internal set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(
                    @"Server=gdagad;Database=eStoreAppDB;Trusted_Connection=True;");
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<ProductCategory>()
                    .HasKey(c => new {c.CategoryId, c.ProductId});  
            }
        }

SeedDatabase.cs;

1

There are 1 answers

2
Michael Wang On

Use the EF Core Migrations feature to update the database.

 1. Add-Migration AddUrl

 2. Update-Database

add a new field to an ASP.NET Core MVC app