[英]Update database schema from entities (without using entity framework migration) in .net core
[英]How can I update the database model after doing a migration in .NET Core 3.1 using Entity Framework?
我是新人,原諒我的無知。
我有這個 class:
public class Genre
{
public int Id { get; set; }
[Required]
[StringLength(40)]
[Index(IsUnique = true)] //-----------------------> new change
public string Name{ get; set; }
}
在我的第一個名為Initial
的遷移中,我忘記了使Name
字段唯一的[Index(IsUnique = true)]
屬性。 所以現在我想更新我的數據庫 model 以使此更改生效。
我using System.ComponentModel.DataAnnotations.Schema;
能夠使用unique
屬性。
我運行的命令是:
EntityFrameworkCore\Add-Migration Initial
EntityFrameworkCore\update-database
顯然我不能做類似的事情:
EntityFrameworkCore\Add-Migration change_attribute_name
EntityFrameworkCore\update-database
更新數據庫 model 並且我更新了我正在做的Name
字段上的unique
屬性。
我想用新的更改更新我的數據庫的 model。 我該怎么做? 什么是最好的方法?
注意:我的 SQL 服務器數據庫還沒有數據。
也許我一直做得很好,你知道為什么我的數據庫 model 中沒有添加unique
屬性嗎?
(在西班牙語中 Genero=Genre 和 Nombre=Name)
編輯
在 EF 核心中,您需要在ApplicationDbContext.cs
文件中使用Fluent API以下列方式應用唯一索引:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Genre>()
.HasIndex(u => u.Name)
.IsUnique();
}
正如您所提到的,您在數據庫中還沒有任何數據,您可以通過使用恢復所有應用的遷移
Update-Database -Migration 0
-Migration 0
表示您要取消應用所有遷移。
然后您可以使用刪除生成的遷移
Remove-Migration
最后,使用新的更改再次添加遷移
Add-Migration Initial
更改 model 並嘗試添加遷移
添加遷移您的遷移名稱
然后運行
更新數據庫
命令這肯定會為你工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.