热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:MySqlScaffoldinganExistingDatabaseinEFCore

篇首语:本文由编程笔记#小编为大家整理,主要介绍了MySqlScaffoldinganExistingDatabaseinEFCore相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了MySql Scaffolding an Existing Database in EF Core相关的知识,希望对你有一定的参考价值。



官方文档详见:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-example.html





Scaffolding a Database Using Package Manager Console in Visual Studio






  1. Open Visual Studio and create a new Console App (.NET Core) for C#.



  2. Add the MySQL NuGet package for EF Core using the Package Manager Console. For example, use the following command to add theMySql.Data.EntityFrameworkCore v8.0.13 package:


    Install-Package MySql.Data.EntityFrameworkCore -Version 8.0.13



    Note

    The version (for example, -v 8.0.13) must match the actual Connector/NET version you are using. For current version information, seeTable 9.2, “Supported versions of Entity Framework Core”.




  3. Install the following NuGet packages by selecting either Package Manager Console or Manage NuGet Packages for Solution from the Tools and then NuGet Package Manager menu:




    • Microsoft.EntityFrameworkCore.Design

      EF Core 1.1 only: Also add the MySql.Data.EntityFrameworkCore.Design package.



    • Microsoft.EntityFrameworkCore.Tools version 1.1.6 (for EF Core 1.1) and Microsoft.EntityFrameworkCore.Tools version 2.0.3 (for EF Core 2.0)



      Note

      The .NET tools are included in the .NET Core 2.1 SDK and not required or supported for EF Core 2.1. If this is an upgrade, remove the reference to that package from the .csproj file (version 2.0.3 in this example) :


      Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />







  4. Open Package Manager Console and enter the following command at the prompt to create the entities and DbContext for the sakila database (adjust the connection-string values to match your settings for the user= and password= options):


    Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir sakila -f

    Visual Studio creates a new sakila folder inside the project, which contains all the tables mapped to entities and the sakilaContext.cs file.




推荐阅读
author-avatar
糖糖菓子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有