What is the Entity Framework
Entity Framework (EF) is a object-relational mapper that enables. NET developers to work with relational data using Domai N-specific objects. It eliminates the need for most of the data-access code, developers usually need to write.
Go from MSDN https://msdn.microsoft.com/en-us/data/ef.aspx?f=255&MSPPError=-2147217396
The Entity framework can be used to manipulate databases
First we're going to get EF from NuGet
You can install the latest version of EF by entering Install-package entityframework in Console Manager
EF default Connection Local db Here we need him to support MySQL
First we need to get mysql.data and Mysql.Data.Entity from NuGet.
Then create the class
public class Mysqldbconfiguration:dbconfiguration {public mysqldbconfiguration () { Sethistorycontext ( "MySql.Data.MySqlClient", (conn, schema) = new Mysqlhistorycontext (conn, schema)); } }
public class Mysqlhistorycontext:historycontext {public mysqlhistorycontext ( DbConnection ExistingConnection, string defaultschema) : Base (existingconnection, Defaultschema) { } protected override void Onmodelcreating (Dbmodelbuilder modelBuilder) { base. Onmodelcreating (ModelBuilder); Modelbuilder.entity
public class Extmysqlmigrationsqlgenerator:mysqlmigrationsqlgenerator {protected override System.Data.Entity. Migrations.Sql.MigrationStatement Generate (System.Data.Entity.Migrations.Model.AddForeignKeyOperation op) { if (Op. Name.length >) op. Name = "fk__" + Op. Name.substring (Op. NAME.LENGTH-60, 60); var ret = base. Generate (OP); return ret; } protected override System.Data.Entity.Migrations.Sql.MigrationStatement Generate (system.data.entity.migrations.m Odel. Renametableoperation op) {var str = base. Generate (OP); Str. SQL = str. Sql.replace ("dbo.", ""); return str; } protected override System.Data.Entity.Migrations.Sql.MigrationStatement Generate (system.data.entity.migrations.m Odel. Dropforeignkeyoperation op) {var str = base. Generate (OP); Str. SQL = str. Sql.replace ("dbo.", ""); return str; } protected Override System.Data.Entity.Migrations.Sql.MigrationStatement Generate ( System.Data.Entity.Migrations.Model.DropPrimaryKeyOperation op) {var str = base. Generate (OP); Str. SQL = str. Sql.replace ("dbo.", ""); return str; } protected override System.Data.Entity.Migrations.Sql.MigrationStatement Generate (system.data.entity.migrations.m Odel. Addprimarykeyoperation op) {var str = base. Generate (OP); Str. SQL = str. Sql.replace ("dbo.", ""); return str; } }In this way, EF can support MySQL database, because Microsoft SQL Server is too bloated, so we chose the lightweight MySQL. Here we are using MARIADB, the open source database developed by the MySQL author, like MySQL, and without worrying about the other effects of MySQL being acquired.
EF is used to interact with the database, we can easily interact with the database through EF, and in the framework of the ASP. NET MVC, the default is to install EF, which is visible ef is still popular, the next chapter will write how to use EF
Ingenious, there is fun.
么么么?
C # Blog Essay Eight: The first knowledge of entityframework