Two, reference DLL:
1, the use of NuGet installation EF6.0.2;
2. Install MySql.Data.Entity.EF6 with NuGet
Note: To install with NuGet, the appropriate DLL or configuration information may be missing
Ii. configuring Web. config or app. Config
1. Replace the Entitframework node with the following:
<entityframework codeconfigurationtype= "MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6" >
<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantname= "MySql.Data.MySqlClient" type= "MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6 "/>
<provider invariantname= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver "/>
</providers>
</entityFramework>
2. Add ConnectionString node:
<connectionStrings>
<add name= "mycontext" connectionstring= "Data source=localhost;port=3306;initial catalog= database name; user id= MySQL login user name; password=mysql server password; "providername=" MySql.Data.MySqlClient "/> Connection to MySQL database
<add name= "TestDB" connectionstring= "Data source=.;i Nitial catalog=mytest; User Id=sa; password=123456; "Providername=" System.Data.SqlClient "/> MSSQL Database
</connectionStrings>
Three model Building
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ComponentModel.DataAnnotations;
Using System.ComponentModel.DataAnnotations.Schema;
Namespace Dbmodel
{
[Table ("UserInfo")]
public partial class UserInfo
{
[Key]
[Column (TypeName = "uniqueidentifier")]
Guid ID {get; set;}
[Column (TypeName = "nvarchar")]
[MaxLength (50)]
String Username{get;set;}
[Column (TypeName = "nvarchar")]
[MaxLength (50)]
[DataType (Datatype.password)]
string Password {get; set;}
}
}
Four establishing the data context
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data.Entity;
Using Dbmodel;
Using System.Data.Entity.ModelConfiguration.Conventions;
Using System.Data.Entity.Infrastructure;
Namespace DB
{
public partial class DBContext:D Bcontext
{
Public DBContext ()
: Base ("Name=testdb")
{
}
Dbset<userinfo> UserInfo {get; set;}
protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Modelbuilder.conventions.remove<pluralizingtablenameconvention> ();//The contract to remove the plural table name does not allow the table name to be plural by default to Userinfos Modelbuilder.conventions.remove<includemetadataconvention> ();//Prevent shady transactions or visit edmmetadata this table every time
}
}
}
Entity Framework Code First implements database