EF's Dbfirst database-in-advance

Source: Internet
Author: User

in the previous article, the macro understanding of the concept and type of EF, the following is about Dbfirst (database first) detailed steps.

Databasefirst is a database-centric approach to development, and using this model we must first design and create a database, Then using VS to create the ADO Entity Data Model based on the existing database, you can then use EF to access and manipulate the data in the database during the programming process, using it in a simple example.

Because it is the database first to look at the university cloud Platform database it:



This is a list of my freshman enrollment configuration numbers, let's build a simple console program:

Open VS2012 and select the console program:

then add the ADO entity model to the project:



Right-click Add, select New item:




Select the ADO entity model:




Select the model type:




If you are creating a new EF model for the first time, take a look at the following steps:



as shown in the following:




after the test is successful, click OK:




This includes tables, views, and so on. Because I want to create a table (you can also build multiple tables)




Click Done and that's what we want:




Finally I synchronize the data in the database by manipulating the entity, which is the data content in the database table before I manipulate it:


Write code:

<pre name= "code" class= "CSharp" >using system;using system.collections.generic;using system.linq;using        System.text;using System.threading.tasks;namespace dbfirst{class Program {static void Main (string[] args)            {freshentities DbContext = new Freshentities ();            Freshnumberconfigentity freshnumber = new freshnumberconfigentity ();            All properties must be assigned a value of Freshnumber.name = "Xu Zhipeng";            Freshnumber.id = 1;            Freshnumber.sort = 3;            Freshnumber.isuse = 0;            Freshnumber.length = 3;            Freshnumber.timespan = DateTime.Now;            Freshnumber.isdelete = 0; The entity's tracking then modifies the entity state dbcontext.entry<freshnumberconfigentity> (Freshnumber).            state = System.Data.EntityState.Modified;            Entity queried: The default is the tracked state//var item = DbContext.FreshNumberConfigEntity.FirstOrDefault (); Item. Name = "College Code";//As long as the property is changed, then the state of this entity is automatically changed to modify Dbcontext.savechanges ();       }    }} 


compile the execution and look at the data in the database:




The above demo is just a small function of the modification, EF's powerful mechanism still waits for the reader hands-on practice and operation!

So we can realize the entity and database synchronization by adding or deleting entities, is there a feeling of being tall?



EF's Dbfirst database-in-advance

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.