C # Blog Essay Eight: The first knowledge of entityframework

Source: Internet
Author: User

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

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.