zoukankan      html  css  js  c++  java
  • 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所在项目的配置文件。

  • 相关阅读:
    js Math对象
    extjs 获取Dom对象
    easyui validatebox 验证集合
    Ext.Ajax.request与form.submit的用法区别
    js获取url参数值
    【原创】extjs4做的grid,带分页,搜索
    SqlServer2005数据库同步
    【原创】jquery实现动态多组图片切换
    easyui表单数据验证
    对象模型图【OMD】阅读指南
  • 原文地址:https://www.cnblogs.com/evlon/p/entity_mysql.html
Copyright © 2011-2022 走看看