首頁>Club>
17
回覆列表
  • 1 # 小小猿愛嘻嘻

    當然是可以的,只需安裝MySQL for VS Code這個外掛就行,下面我簡單介紹一下這個外掛的安裝和使用過程,主要內容如下:

    成功連線mysql後,效果如下,會顯示出當前連線下所有的資料庫:

    3.最後,我們就可以建立sql檔案,進行sql操作了,如下,右擊對應資料庫,選擇“New Query”,建立一個sql查詢:

    接著我們就可以輸入相關的sql語句了,完成後,右鍵滑鼠,選擇“Run MySQL Query”,就會執行當前的sql指令碼,執行結果也會在介面右邊顯示出來,如下:

    至此,我們就完成了在VS Code中使用MySQL資料庫。總的來說,整個過程不難,最主要的就是安裝MySQL for VS Code這個外掛,之後再連線資料庫,就能進行正常的資料庫操作了(增刪改查等),只要你熟悉一下相關操作,很快就能掌握了,當然,VS Code官方也已經給出了MySQL外掛的詳細使用過程了,如下,照著來就行,網上也有相關教程和資料,感興趣的可以搜一下,希望以上分享的內容能對你有所幫助吧。

  • 2 # 新無止競

    一、Mysql的安裝

    首先肯定是安裝VS,這都不必再說的。其次是下載Mysql,網址http://dev.mysql.com/downloads/installer/

    開啟會看到下面的兩行

    If you have an online connection while running the MySQL Installer, choose the mysql-installer-web-community file. If you do NOT have an online connection while running the MySQL Installer, choose the mysql-installer-community file.

    以我4級差一分的英文水平來翻譯一下就是如果聯網安裝可以下載mysql-installer-web-community,不是則下載mysql-installer-community

    下載安裝之後會出現下面的頁面

    這裡我是透過自定義選擇的,上面我選擇了3個檔案,第一個是Mysql資料庫,第二個算是mysql給VS提供的操作工具,第三個就是Mysql的客戶端,可以視覺化的操作資料庫。這裡要說下Mysql Connectors,如果使用類似ado.net的方式可以直接安裝Connectors就可以。下載之後一次下一步安裝,然後配置。

    二、VS Code First使用Mysql

    1.上面安裝Mysql for vs之後就會在vs新建項時出現下面的內容,如果不安裝是不會出現的。

    2.類庫的引入

    EF框架操作Mysql資料庫需要透過NuGet引入MySql.Data.Entity.EF6

    引入之後會出現兩個dll引用,下圖的EF引用是在新增實體ADO.Net實體資料模型時自動新增的。

    3.配置檔案

    光引入這些還不夠,還有修改配置檔案,因為預設的是sql server資料庫,所以需要修改配置檔案。將系統生成的配置檔案修改為下面的內容。這裡注意引用的版本號

    <?xml version="1.0" encoding="utf-8"?>

    <configuration>

    <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    </configSections>

    <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6">

    <parameters>

    <parameter value="v11.0" />

    </parameters>

    </defaultConnectionFactory>

    <providers>

    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>

    </providers>

    </entityFramework>

    <connectionStrings>

    <add name="TestContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=TestContext;user id=admin;password=123456;" providerName="MySql.Data.MySqlClient" />

    </connectionStrings>

    <system.data>

    <DbProviderFactories>

    <remove invariant="MySql.Data.MySqlClient" />

    <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />

    </DbProviderFactories>

    </system.data>

    </configuration>

    4.應用

    下面是我為這個做了一個簡單的demo。首先是新建了一個window控制檯應用程式,然後新增ADO.NET實體資料模型的空的Code First。然後在資料庫上下文中增加實體User。同時設定連線字串是TestContext。

    namespace EFMysql

    {

    using System;

    using System.Data.Entity;

    using System.Linq;

    public class TestContext : DbContext

    {

    public TestContext()

    : base("name=TestContext")

    {

    }

    public virtual DbSet<User> Users { get; set; }

    }

    }

    using System;

    using System.Collections.Generic;

    using System.ComponentModel.DataAnnotations;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace EFMysql

    {

    public class User

    {

    [Key]

    public int UserId { get; set; }

    public string Name { get; set; }

    }

    }

    在控制檯中連線資料庫新增一個User實體物件。

    static void Main(string[] args)

    {

    using (var db = new TestContext())

    {

    User user = new User() { Name = "Test" };

    db.Users.Add(user);

    db.SaveChanges();

    Console.WriteLine("OK");

    }

    Console.ReadLine();

    }

    上面要注意的是Console.ReadLine();不能寫在using裡面,不然就一閃而過。下面是輸出的OK,然後檢視Mysql服務可以發現有一個名TestContext的資料庫

  • 中秋節和大豐收的關聯?
  • 18歲的男生臉上痘痘多,面板出油嚴重,有哪些合適的護膚品可以推薦?