I just have an existing database written script in MYSQL and I just want to import all the tables into my asp.net core 2.0 web application with entity framework core. Also, I want to use database first approach into my project so that all tables should add to the project with poco classes. So please help me out in this situation.
2
votes
2 Answers
-1
votes
1. Run the following commands:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package MySql.Data.EntityFrameworkCore.Design
2. Add a reference to Microsoft.EntityFrameworkCore.Tools.DotNet
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
3. Scaffold the Database
3.1 Install the following NuGet packages
MySql.Data.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
3.2 Scaffold
Scaffold-DbContext `"server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir sakila -f`
-1
votes
Entity Framework core works on Code First approach. This solution is for SQL Server.You can try the below steps.
- You run the script against the database.
- Download and install EF Core Power Tools.
- Follow the mentioned instructions, It will generate all the model POCO classes in your C# projects.
- Run
Add-Migration
(don't forget to add the POCO classes in your DBContext), you will see the changes in the generated migration. So you have two options either you can delete the database manually and then runUpdate-Database
command or you can remove the auto-generated code inUp
method of your generated migration.
You can find the tool on GitHub also.
Note: For Trigger and Stored Procedures you have to manually tun the Script in the generated migration.
Steps for Stored Procedure:
- Add a File with Stored Procedure syntax like
Alter
orCreate
Procedure. - Run
Add-Migration
and generate empty migration. Add the similar code in your
Up
method.RunScript(migrationBuilder, @"PathToYouScript"); private void RunScript(MigrationBuilder migrationBuilder, string scriptFilePath) { var migratorPath = Path.Combine("..", scriptFilePath); if (!File.Exists(migratorPath)) { migratorPath = Path.Combine("..", "..", "..", "..", scriptFilePath); } migrationBuilder.Sql(File.ReadAllText(migratorPath)); }
- Run
Update-Database
.