EF6 Codefirst + MySql database migration, ef6codefirst

Source: Internet
Author: User
Tags mysql for visual studio

EF6 Codefirst + MySql database migration, ef6codefirst
Introduction

The project uses MSSql as the database, but because the SQL Server is expensive and the concurrent connection is poor, it is necessary to migrate data to MySQL, along with the migration process and problems. Environment

· Visual maxcompute 2013

· MySQL 5.7

· Entity Framework 6.1.3 body MIGRATION PROCESS

1. Install MySQL, along with MySQL for Visual Studio and MySQL Connector Net

2. add the Nuget package, MySql. data. entity. I have added version 6.9.6 here. MySQL will be added. data is added to the project as a dependency. config and app. mySQL provider and DbProvideFactories are added under config.

<entityFramework>    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">      <parameters>        <parameter value="v11.0" />      </parameters>    </defaultConnectionFactory>    <providers>      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />      <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>
<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>

3. Modify the Web. config connection string and providerName to MySql. Data. MySqlClient.

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=Demo;User ID=root;Password=root" />

4. Return to the EntityFramework project, find the DbContext class, and add the attribute to the class: [DbConfigurationType (typeof (MySql. Data. Entity. MySqlEFConfiguration)]

   [DbConfigurationType (typeof(MySql. Data. Entity. MySqlEFConfiguration)]Public class MyDbContext: DbContext {// <summary> // set the database connection corresponding to the data entity /// </summary> public MyDbContext (): base ("DefaultConnection ") {} // The OnModelCreating method protected override void OnModelCreating (DbModelBuilder modelBuilder) {base must be rewritten using the Fluent api. onModelCreating (modelBuilder); modelBuilder. conventions. remove <PluralizingTableNameConvention> (); // Remove the contract for the complex table name }}

 

5. OK. after the work is done, you can simply open the Package Manager Console, pray carefully, and enter update-database.

Migration Process Problems

1. The object reference is not set to the instance of the object.

Problem description: this is a common Error prompt. Many errors will prompt this. I found some errors online. I found that the startup Item is set to EF project, and the default project is EF project, the connection string is not found in the config file.

Solution: Add a connection string in app. config of EF, or set the startup project to WebSite.

2. underlying provider does not support the type 'nvarchar (max )'.

Problem description: literally, provider does not support the "nvarchar (max)" type, which is related to the data types supported by MySQL database.

Solution: Check whether the Entity definition contains the string type, add the attribute such as [StringLength (1000)], and solve the problem.

 

 

3. The provider does not return the ProviderManifestToken string.

Cause: the connection string is incorrect.

Correct syntax: data source cannot be replaced.

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=demo;User ID=root;Password=root" />

4. The member "MySql. Data. MySqlClient. MySqlException, MySql. Data, Version = 6.9.9.0, Culture = neutral, PublicKeyT" is not resolved

System.Runtime.Serialization.SerializationException:  
The "MySql. Data. MySqlClient. MySqlException, MySql. Data, Version = 6.9.9.0, Culture = neutral, PublicKeyToken = c5687fc88969c44d" type is not parsed. In System. appDomain. doCallBack (CrossAppDomainDelegate callBackDelegate) in System. data. entity. migrations. design. toolingFacade. run (BaseRunner runner) in System. data. entity. migrations. design. toolingFacade. update (String targetMigration, Boolean force) in System. data. entity. migrations. updateDatabaseCommand. <> c _ DisplayClass2. <. ctor> B '_ 0 () in System. data. entity. migrations. migrationsDomainCommand. execute (Action command)
The "MySql. Data. MySqlClient. MySqlException, MySql. Data, Version = 6.9.9.0, Culture = neutral, PublicKeyToken = c5687fc88969c44d" type is not parsed.

Solution: It usually happens from the original development environment to the new development environment. In the development environment, the "mysql-connector-net" is missing. Download and install it.

5. Starting from index 0, the format of the initialized string does not conform to the specification.

Problem description: this problem is similar to the first one, but I changed the prompt mode because I have set the project containing the connection string as the startup item in the project, this causes a lack of connection strings in webconfig.

PM> update-database: Specify the "-Verbose" flag to view the SQL statements applied to the target database. System. ArgumentException: Starting from index 0, the format of the initialized string is invalid. In System. data. common. dbConnectionOptions. getKeyValuePair (String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String & keyname, String & keyvalue) in System. data. common. dbConnectionOptions. parseInternal (Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) in System. data. common. dbConnectionOptions .. ctor (String co NnectionString, Hashtable synonyms, Boolean useOdbcRules) in System. data. common. dbConnectionStringBuilder. set_ConnectionString (String value) in MySql. data. mySqlClient. mySqlConnectionStringBuilder .. ctor (String connStr) in MySql. data. mySqlClient. mySqlConnection. set_ConnectionString (String value) in MySql. data. mySqlClient. mySqlConnection .. ctor (String connectionString) in MySql. data. entity. mySqlConnectionF Acloud. createConnection (String connectionString) in System. data. entity. internal. lazyInternalConnection. initialize () in System. data. entity. internal. lazyInternalConnection. get_Connection () in System. data. entity. internal. lazyInternalContext. get_Connection () in System. data. entity. infrastructure. dbContextInfo .. ctor (Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo conne CtionInfo, Func '1 resolver) in System. data. entity. migrations. dbMigrator .. ctor (DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) in System. data. entity. migrations. dbMigrator .. ctor (DbMigrationsConfiguration configuration) in System. data. entity. migrations. design. toolingFacade. baseRunner. getMigrator () in System. data. entit Y. migrations. design. toolingFacade. updateRunner. run () in System. appDomain. doCallBack (CrossAppDomainDelegate callBackDelegate) in System. appDomain. doCallBack (CrossAppDomainDelegate callBackDelegate) in System. data. entity. migrations. design. toolingFacade. run (BaseRunner runner) in System. data. entity. migrations. design. toolingFacade. update (String targetMigration, Boolean force) in System. data. entity. migration S. updateDatabaseCommand. <> c _ DisplayClass2. <. ctor> B '_ 0 () in System. data. entity. migrations. migrationsDomainCommand. execute (Action command) starts from index 0 and the format of the initialization string does not conform to the specification.

Solution: set the project containing the connection stringStart the project, Set the project that contains dbContext to the default migration project, which isDefault Project.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.