1. New Project
2. Create a folder for the class Dal
3. Create related classes
"Student Class"
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace ETTest3
{
public class Student
{
public int Id {get; set;}
public string LastName {get; set;}
}
}
"School Class"
Using System;
Using System.Collections.Generic;
Using System.Data.Entity.ModelConfiguration.Conventions;
Using System.Data.Entity;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace ETTest3
{
public class Schoolcontext:dbcontext
{
Public Schoolcontext (): Base ("Mycontext") {}
Public dbset<student> Students {get; set;}
protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Base. Onmodelcreating (ModelBuilder);
}
}
}
4. Reference EF6.0 mysql.entity
5. Adjust the configuration File App. Config
<connectionStrings>
<add name= "mycontext" connectionstring= "Data source=localhost;port=3306;initial catalog=test;user id=root; password=123456; "Providername=" MySql.Data.MySqlClient "/>
</connectionStrings>
<entityFramework>
<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<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.8.0, culture=neutral, publickeytoken=c5687fc88969c44d ">
</provider></providers>
</entityFramework>
6 Test Build Database
Try
{
Schoolcontext CTX = new Schoolcontext ();
CTx. Database.initialize (TRUE);
var o = new Student ();
O.id = 1;
O.lastname = "AA";
CTx. Students.add (o);
CTx. SaveChanges ();
}
catch (Exception ex)
{
Console.Write (ex. Message);
}
7. Test Success , open the database, you can see MySQL added a library test, there is a table student
8 Processing of field changes in a data table
If the change is relatively small, such as adding or removing one or two fields
First back up the database directly after the data table operation, your data model and database table to do the corresponding on it,
It should be noted that the project on-line generally need to set DataContext
Database.setinitializer<datacontext> (NULL);
and cannot be set
Database.setinitializer<datacontext> (New dropcreatedatabaseifmodelchanges<datacontext> ());
Database.setinitializer<datacontext> (New dropcreatedatabasealways<datacontext> ()); and so on other ways,
Prevent the database from being accidentally deleted and rebuilt (because the field you added might not be the same as your model map)
If the changes are very large, you might want to switch to another scenario, migrate data, or import data yourself.
Use EF6.0 to build MySQL database on VisualStadio2015