如何使用EF CORE 的first code 來連接已有的數據庫

首先安裝EF的兩個必要的包

> dotnet add package Microsoft.EntityFrameworkCore.SqlServer
> dotnet add package Microsoft.EntityFrameworkCore.Design

在vs下運行命令Scaffold-DbContext "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB1;Data Source=SERVER2016;uid=sa;pwd=abc123#" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context "AdventureContext" -DataAnnotations 

也可以選擇table, -Tables "Pub_InternalControlDes","Pub_InternalControlMap" 

生成model後把

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            
        }

裏的都刪掉,然後運行命令add-migration test;

udapte database,

把migration裏面的up和down的方法都去掉,就完成了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章