Summary of advantages and disadvantages and usage of Entity Framework

Source: Internet
Author: User
Tags configuration settings
Entity Framework advantages and disadvantages and usage summary Entity Framework is an ORM framework provided by Microsoft. It aims to facilitate the rapid development of data layers in small applications.

More than downloads on nuget indicate that. NET developers prefer to use EF. However, EF also has many disadvantages while providing convenience. I think EF should not be applied in the following scenarios:

 

  • Dataprovider of a non-SQL Server database without the database
  • High performance requirements. In the case of complex queries, EF performance is not good, and developers cannot control the generation of SQL statements.
  • High security requirements. Sometimes dB users only have the exec permission, but the classes automatically generated by EF are not easy to use. You still need to write them by yourself.

 

Some large and medium-sized enterprise applications often have the above situations, so EF is not suitable for use at this time.

As for what ORM to use, I personally think it is too troublesome to configure Nhibernate. If there are no special requirements, you can try servicestack. ormlite to meet your needs. Or directly go to ADO. net.

 

To use EF, we recommend that you use the latest stable release version of EF (dbcontext + code first supported ).

Then read the two books programming Entity Framework code first and programming Entity Framework dbcontext (no Chinese version currently ).

Dbcontext + code first poco + repository mode is used to ensure that EF is isolated by repository and testability. You can also change the ORM framework or even change it to ADO. net.

 

Download Sample Code (. NET 4 + EF5)

 

In the sample code, there are poco and EF deployments. The ef-related deployments are taken out separately to ensure the purity of the DB model. Here, let's take a look at some things.

 

The first is the database structure. I use code first. The database will be automatically generated when an actual database operation is performed for the first time. The generated database structure is as follows:

 

The database Generation Policy is in the static structure of the efcontext class:

Static efcontext () {// specify the database generation policy to be generated if it does not exist // You can also specify it as: // dropcreatedatabasealways (always delete the original database and recreate it) // dropcreatedatabaseifmodelchanges (delete the original database after the database model changes) database. setinitializer <efcontext> (New createdatabaseifnotexists <efcontext> ());}

 

Configuration settings of the album table:

Public class albumconfiguration: entitytypeconfiguration <album>

{

  • Public albumconfiguration ()
  • {
  • // Specify the data table name
  • Totable ("DBO. Album ");
  • // Set the primary key
  • Haskey (x => X. ID );
  • // Set the data column name and other attributes
  • Property (x => X. ID). hascolumnname ("ID"). hasdatabasegeneratedoption (databasegeneratedoption. Identity). isrequired ();
  • Property (x => X. albumname). hascolumnname ("albumname"). isrequired (). hasmaxlength (50 );
  • Property (x => X. artistid). hascolumnname ("artistid"). isrequired ();
  • // Set foreign key constraints
  • Hasrequired (foreignkeytable => foreignkeytable. Artist). withmany (primarykeytable => primarykeytable. albums). hasforeignkey (foreignkeytable => foreignkeytable. artistid );
  • // Set multiple-to-multiple associations
  • Hasmany (thistable => thistable. tags ). withmany (anotherforeignkeytable => anotherforeignkeytable. albums ). map (relationtable => relationtable. mapleftkey ("tagid "). maprightkey ("albumid "). totable ("albumtags "));
  • }

}

 

Override the onmodelcreating function in the efcontext class and use our custom mappings:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  • Base. onmodelcreating (modelbuilder );
  • // Configure database generation
  • Modelbuilder. configurations. Add (New artistconfiguration ());
  • Modelbuilder. configurations. Add (New albumconfiguration ());
  • Modelbuilder. configurations. Add (New tagconfiguration ());
}

 

I will not talk about the repository mode here. For details, refer to the content in the abstractdbaccessor project,

EF repository implements the efdbaccessor and repository directories under the concretedbaccessor project entityframework directory

Summary of advantages and disadvantages and usage of Entity Framework

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.