簡體   English   中英

我無法更新實體

[英]I cannot update entity

類別表;

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

我在表中添加了新字段;

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

更新實體時出現以下錯誤; Microsoft.EntityFrameworkCore.DbUpdateException: '更新條目時出錯。 有關詳細信息,請參閱內部異常。

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

種子數據庫.cs;

使用 EF Core 遷移功能更新數據庫。

 1. Add-Migration AddUrl

 2. Update-Database

向 ASP.NET Core MVC 應用程序添加新字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM