Entityframework codefirst 4.3 step by step (1)

Source: Internet
Author: User

Since codefirst was launched, I have always wanted to learn and use it. I have been suffering from various things (of course this is not a reason !) But delay...

I recently moved to a German enterprise. after entering the project team, I mainly worked on a software program for Ship Information Management and control. The customer was Norwegian. Codefirst4.2 is used in the project. I really feel that foreigners are keeping pace with the times !!! So I can't help it anymore. I want to have a good time playing codefirst. In order to urge myself to learn, I want to assure all the friends in the garden that I will write the codefirst series completely, please supervise and spur me, haha!

In the following time, I will learn codefirst4.3 with the following questions:

1. What's the advantage of codefirst.

2. How to config and use.

3. How to migrate.

4. How to Use in project.

 

 

Next we will officially start our codefirst Journey:

1. What's the advantage of codefirst.

Codefirst, as its name implies, isCodeFirst, it abandons the process of traditional project development models, such as ER Model and DB Design, so that we can start development directly from domainmodel, that is, domain model. Without the database design, weProgramAfter every communication, members and domain experts can quickly abstract the domain model and implement the corresponding functions. Then, we can use the functions provided by codefirst to generate a database in reverse order.

From this process, we can see that codefirst has the following advantages:

Development Speed: At this time, our programmers do not need to possess DBA-related skills and directly use code (the programmer's favorite ).

Clear and controllable code: There is no entity class automatically generated by entityframework. Instead, we designed our own domainmodel based on the domain model.

Goodbye to edmx model: Do you still remember edmx model? Every time there is a change to the database, we need to maintain this thing, if developers are connected to the same database, database updates and edmx model updates may often be out of sync (my previous colleagues in the company should be quite familiar with this ).

 

2. How to config and use.

Configuration and use are everywhere on the Internet, but almost all of them are in the same version. I don't know which one is in the wholesale market. I use SQL Express, in our actual project, there are few complete demos for SQL Server instances.

Let's talk about the specific configuration and usage of codefirst4.3.

1. Add the reference of entityframework4.3. If no nuget is installed, everything will be available.

2. Is the system. componentmodel. dataannotations referenced by system. Data. entity added? If you don't have one, add it!

3. The reference to the system. Data. sqlserverce. entity. dll assembly is definitely not available. It's not for programmers to say they cannot find it. If they can't find it, simply email to me, I can send you!

4. After these references are added, modify the config file (which can be app. config or web. config based on the project type)

App. config

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Configuration  >  3     <  Configsections  >  4       <! -- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink? Linkid = 237468  -->  5       <  Section  Name  = "Entityframework"  Type  = "System. Data. entity. Internal. configfile. entityframeworksection, entityframework, version = 4.3.1.0, culture = neutral, publickeytoken = b77a5c561934e089"   />  6     </  Configsections >  7     <! --  <Entityframework>  8   <Defaconnecticonnectionfactory type = "system. Data. entity. Infrastructure. sqlconnectionfactory, entityframework">  9   <Parameters>  10   <Parameter value = "Data Source =. \ sqlexpress; Integrated Security = true; multipleactiveresultsets = true"/>  11   </Parameters>  12  </Defaultonfactory>  13   </Entityframework>  -->  14   15     <  Connectionstrings  >  16       <  Add  Name  = "Blogdb"  Connectionstring = "Data Source =.; initial catalog = blogdb; user id = sa; Password = 123"  Providername  = "System. Data. sqlclient"   />  17     </  Connectionstrings  >  18     <  System. Data  >  19       <  Dbproviderfactories  > 20         <  Remove  Invariant  = "System. Data. sqlserverce.4.0"   />  21         <  Add  Name  = "Microsoft SQL Server compact data provider 4.0"  Invariant  = "System. Data. sqlserverce.4.0"  Description = ". NET Framework data provider for Microsoft SQL Server compact"  Type  = "System. Data. sqlserverce. sqlceproviderfactory, system. Data. sqlserverce, version = 4.0.0.0, culture = neutral, publickeytoken = 89845dcd80cc91"   />  22       </  Dbproviderfactories  >  23     </  System. Data  >  24   </ Configuration  > 

I have been tossing for more than an hour. I don't want to explain it. It's correct to do it!

5. Configure OK. Now you can start writing code comfortably!

Suppose we create a blog system, discuss it with the domain experts, and abstract a domainmodel as blog.

Blog

1ClassBlog2 {3Public IntBlogid {Get;Set;}4Public StringName {Get;Set;}5}

Based on previous experience, this should be a table in the database. How can we generate a blogs table?

We need a class that inherits from dbcontext and has an attribute.

Blogcontext

1       Class  Blogcontext: dbcontext  2   {  3           Public  Blogcontext ()  4 : Base ( "  Blogdb  " ) //  The name shall be equal to the name of connectionstring in config file. 5   {  6   }  7   8           Public Dbset <blog> blogs { Get ; Set  ;}  9 }

I am using a console program, so I will perform a database insertion Operation in the main function.

Program

 1       Class  Program 2   {  3           Static   Void Main ( String  [] ARGs)  4   {  5               Using ( VaR DB = New  Blogcontext ())  6   { 7 DB. Blogs. Add ( New Blog {name = "  Another blog  "  });  8   DB. savechanges ();  9   10                   Foreach ( VaR Blog In  DB. Blogs)  11  {  12   Console. writeline (blog. Name );  13   }  14   15   Console. Readline ();  16   }  17   }  18 }

Open the database and blogdb is lying down!
So far, we have tried codefirst, but the problem is that if I add a user class, do I have to perform a database operation?

Codefirst4.3 is certainly not so weak. It provides powerful migration functions to update the database, so that we can stay away from the pain of maintaining the edmx model in the entityframework era. At the same time, it also changes the environment of database and code synchronization changes caused by demand changes.

In the next section, we will talk about migration.

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.