簡體   English   中英

“ update-database”命令未更新基礎數據庫中的模型

[英]“update-database” Command not updating the model in the underlying Database

之前我有這兩個類,我為此創建了遷移,並生成了數據庫表。 但是我必須按特定順序向Type添加另一個屬性,因此我創建了一個空遷移,並使用“ sql”方法並由於外鍵約束而刪除了這兩個表。 現在,我對“類型”類進行了所有更改。 所以我正在使用update-database commmand EntityFramework不選擇這些類?

給定的類=>“更改前”。

public class Type
{
    public int Id { get; set; }
    public int SignUpFee { get; set; }
    public int Discount { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Type Type { get; set; }
    public int TypeId { get; set; }
}

因此,基本上,您需要從表__MigrationHistory中刪除與這些表相關的較早遷移記錄,並從Migration文件夾中刪除較早相關的遷移文件以用於將來的遷移運行(聲明),並針對這些類添加映射,例如:

public class TypeMapping : EntityTypeConfiguration<Type>
    {
        public TypeMapping()
        {
            // Keys
            HasKey(t => t.ID);

            //Property
            Property(t => t.SignUpFee);
            Property(t => t.Discount);            

            //Table
            ToTable("Type");
        }
    }

public class CustomerMapping : EntityTypeConfiguration<Customer>
        {
            public CustomerMapping()
            {
                // Keys
                HasKey(t => t.ID);

                //Property
                Property(t => t.Name);

                //Table
                ToTable("Customer");
            }
        }

然后,運行以下命令:

1)添加遷移“ MigrationName” 2)更新數據庫

暫無
暫無

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

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