Sometimes you need to rent a space or database online, MySQL cost is lower, so you want to turn SQL Server into MySQL ...
Note: When you install MySQL, select the text set as UTF8, otherwise you will not be able to use Chinese (you can also use UTF8 when you create the database, but I do not know how to set it when EF builds the database, I hope the expert pointing)
One, the EF package that references MySQL in the project
Install via NuGet Package Manager: EntityFramework6.1.3, mysql.data.entity6.9.8 can also be added with NuGet's command line:
Install-Package MySql.Data.Entity
Second, the new related classes
1. New User entity classand define the field length of the instance, undefined words will appear specified key was too Long;max key length is 767 bytes error, this is because the string type directly mapped to MySQL is Longtext, and MySQL Supports a maximum length of 767 bytes. public class User {public int Id {get; set;} [Stringlength] public string UserName {get; set;} [MaxLength] public string PassWord {get; set;} }
2. New Mycontext classand instructions are implemented with MySQL [Dbconfigurationtype (typeof (Mysqlefconfiguration))]
[Dbconfigurationtype (typeof (Mysqlefconfiguration))] public class MyContext : DbContext { Public mycontext () : base (" Name=mycontext ")//web.config connectionstring name { } public dbset <user> users { get; set; } } 3, write test code database.setinitializer (new dropcreatedatabaseifmodelchanges< Mycontext> ()); var Context = new mycontext (); //inserting a row of values context. Users.add (new user { username = "Ef6mysql" &NBSP;}); context. SaveChanges (); Three, configure web.config Add the following code to <connectionStrings>: <add name= "mycontext" connectionstring= "Data source=localhost;port=3306;initial catalog=mysql_ef;user id= root;password=root; "Providername=" MySql.Data.MySqlClient "/>
The complete Web. config is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <configsections> <!--for more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 - < Sectionname= "EntityFramework"type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=6.0.0.0, culture= Neutral, publickeytoken=b77a5c561934e089 "requirepermission= "false" /> </configsections> <EntityFramework> <defaultconnectionfactorytype= "System.Data.Entity.Infrastructure.SqlConnectionFactory, entityframework" /> <providers> <providerInvariantName= "System.Data.SqlClient"type= "System.Data.Entity.SqlServer.SqlProviderServices, entityframework.sqlserver" /> <providerInvariantName= "MySql.Data.MySqlClient"type= "MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, version=6.9.8.0, Culture=neutral, Publickeytoken=c5687fc88969c44d "> </provider> </providers> </EntityFramework> <System.Data> <dbproviderfactories> <Removeinvariant= "MySql.Data.MySqlClient" /> <Addname= "MySQL Data Provider"invariant= "MySql.Data.MySqlClient"Description= ". Net Framework Data Provider for MySQL"type= "MySql.Data.MySqlClient.MySqlClientFactory, Mysql.data, version=6.9.8.0, Culture=neutral, publickeytoken= C5687fc88969c44d " /> </dbproviderfactories> </System.Data> <connectionStrings> <Addname= "Mycontext"connectionString= "Data source=localhost;port=3306;initial catalog=mysql_ef;user id=root;password=root;"ProviderName= "MySql.Data.MySqlClient" /> </connectionStrings></Configuration>
Finally, run the program to complete the database auto-creation FAQ
- Error message: Specified Key was too Long;max key length is 767 bytes
1) See if the String Type property of the entity is set to length 2) whether the Mycontext class declares a living as a MySQL data type [Dbconfigurationtype (typeof (Mysqlefconfiguration))]
- Error message: Model Compatibility cannot is checked because the database does not contain model metadata
Rerun a program after you delete a database that has been built
- Error message: Sequence does not contain any matching elements
Check:
For example: 1.
public class employee{ [Key] public int EmployeeId {get; set;} public string Name {get; set;} [ForeignKey ("ManagerID")] Public Employee Manager {get; set;} public int ManagerID {get; set;}}
public int ManagerID {get; set;}
This foreign key setting.
2.
[Column (Typename= "VARCHAR (254)")]
public string ColumnName {get; set;}
3.(The following code is not tested because I am not using it, and will be tested in the next article)
Modelbuilder.entity<category> () . Haskey (c = c.idcategory) . Hasoptional (p = p.children) . Withmany () . Hasforeignkey (c = c.childrenid); change to:modelbuilder.entity<category> () . Haskey (c = c.idcategory) . Hasmany (p = p.children) . Withoptional () . Hasforeignkey (c = C.childrenid);. Withmany () replaced. Withoptional ()
Ways to use MySQL in EF and frequently asked questions