entityframework使用CodeFirst創建MySql數據庫出錯的解決方法恢復


先告訴大家一個秘密,EF在使用 update-database 時候,使用的連接字符串來自於解決方案中的“啟動項目”,而不是你在包管理器中選擇的“默認項目”

0x01. 先說錯誤,方便大家檢索到

開發環境,VS2015+MySql+EntityFramework6

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="MySql.Data" version="6.9.7" targetFramework="net45" />
  <package id="MySql.Data.Entity" version="6.9.7" targetFramework="net45" />
</packages>

執行數據庫的時候報錯:

PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.SqlClient.SqlException (0x80131904): 在與 SQL Server 建立連接時出現與網絡相關的或特定於實例的錯誤。未找到或無法訪問服務器。請驗證實例名稱是否正確並且 SQL Server 已配置為允許遠程連接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服務器/實例時出錯) 

 這時候我的配置文件:

<?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>
  <connectionStrings>
    <add name="conn.mysql" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1;port=3306;Initial Catalog=moa;user id=root;password=;" />
  </connectionStrings>
  <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <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.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <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.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>

 

0x02,一頭霧水,各種懷疑

一個項目從一個解放方案,移動到另一個,就出錯,BeyongCompare 比較除了應該不一樣的都一樣,我都懷疑EF有一個小數據庫,針對每個項目記錄什么配置了。

0x03,柳暗花明,發現疑點

當執行這個命令:

PM> Enable-Migrations -ConnectionStringName "conn.mysql" -Force -Verbose
Using StartUp project 'Masap.Service.Contract'. Using NuGet project 'Moa.Data'. Checking if the context targets an existing database... System.InvalidOperationException: No connection string named 'conn.mysql' could be found in the application config file. 

 看到紅色字了吧, 我的EF項目是Moa.Data,和Masap.Service.Contract 沒有關系,在結合找不到 "conn.mysql" 連接字符串,我頓悟了。馬上吧解決方案中的啟動項目設置為“Moa.Data",再次運行:

PM> Enable-Migrations -ConnectionStringName "conn.mysql" -Force -Verbose
Using StartUp project 'Moa.Data'.
Using NuGet project 'Moa.Data'.
Checking if the context targets an existing database...
Code First Migrations enabled for project Moa.Data.
PM> Add-Migration Init
Scaffolding migration 'Init'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Init' again.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201509301418201_Init].
Applying explicit migration: 201509301418201_Init.
Running Seed method.
PM> 

 一切正常了。

0x04,總結

EF在使用包管理器執行命令的時候,所有的配置都使用解決方案中”啟動項目“的配置文件(app.config or  web.config)。而不是EF所在項目的配置文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM