Search code examples
c#entity-frameworkentity-framework-6

How I can create migration for SQL queries in Entity Framework 6?


I am using Entity framework 6 code 1st.

I have couple SQL queried as follows:

delete from [dbo].[User];


 insert into [dbo].[User] (id, permission_id)
 values (1, (select [id] from [permission] where [name] = 'write')),
       (......

I need to Add Migration for these Scripts, How I can create migration for SQL queries?


Solution

  • install the ef core tools (reference)

    dotnet tool install --global dotnet-ef
    

    issue a command from the cli to create a migration (reference)

    dotnet ef migrations add InitialCreate
    

    You will also need to have a reference to Microsoft.EntityFrameworkCore.Design in the startup project.

    In a lot of my work I have a web assembly (api) and a separate model/repository assembly, so a typical migration command for me looks like

    dotnet ef migrations add MigraitonName -s StartupProject -p RepositoryProejct -c DbContext
    

    Once you have a migration you can run the following command to update your database

    dotnet ef database update