EF Core 6 Adding a table manually?

Cenk 1,026 Reputation points
2023-01-17T14:19:19.31+00:00

Hello friends,

I am working on a web API project that I implemented with the code-first approach. I wonder if there is a problem creating tables manually without using migrations. Does it cause any problems?

Thank you.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
776 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Nieze Ben Mansour 0 Reputation points
    2023-01-17T15:58:24.3433333+00:00

    Hi Cenk, To create a table manually, you can create the table with SSMS (for example) after that, you scaffold the database to generate the entities.

    This a link for how to use Reverse Enginner: https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli


  2. Jack J Jun 24,641 Reputation points Microsoft Vendor
    2023-01-30T09:37:33.7466667+00:00

    @Cenk T ,Welcome to Microsoft Q&A, we also could use ExecuteSqlRaw method to create the table.

    Here is a code example you could refer to.

    TeacherContext context=new TeacherContext();
    string sqltext = "create table Employee (empno int,empname varchar(50),salary money);";
    context.Database.ExecuteSqlRaw(sqltext);
    

    Result:

    User's image

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.