EF three Programming details tutorial (C#+EF), model first

Source: Internet
Author: User

Model First

Model first is what we call "model precedence", where the models refer to the "ADO. NET Entity Framework Data model", when your application does not design the relevant database, in the visual In Studio we build databases and data classes by designing the data model for the.

First create a console application, right-click Add New Item, select "ADO. Data Model", name input Efdemodb(this name is not associated with the previous one, but onlythe names):

In model Design view, add a new entity: (Right-click in the blank)

Add, two scalar attributes: "Customer" and "OrderDate (for datetime type , which you can select to modify in Properties )"; add second entity in the same way " OrderDetail "and add the" Product "and" UnitPrice "Properties:

Next we add the relationship between the two, "Order" and "OrderDetail" is a one-to-many relationship, "order" through the "OrderDetails" property to access the "OrderDetail" entity, "OrderDetail" can pass " Order property accesses the order entity, and a FOREIGN key constraint is added to "OrderDetail":

After the relationship has been added:

So far the model in model first has been created, the following needs to be built into the database, in the Model Design view blank Place select "from model generation to Database ...": (This is still used in the above EFDemo database, can be changed according to their own needs , New connection Replacement database )

Select the database connection and click Next and you will see the generated SQL statement:

Click Finish, no unexpected words will open the generated script, of course, you may also see the following error, please download the latest SQL Server Data Tool (local VS2012, database sqlserver2008r2 The following prompt, download the update, It is recommended to download the image file directly):

After the model is saved, the database context and entity classes are generated, and the script for creating the table is opened:

Description: If not, see if the model is saved (e.g.)

Open the database script:

Then right-click to select Execute.

Then encode the query for a bit:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.ComponentModel;5 usingSystem.Data;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 9 namespaceEFDemoTen { One     class Program A     { -         Static voidMain (string[] args) -         { theOrder o =NewOrder () {Customer ="", OrderDate = DateTime.Now}; Efdemodbcontainer db =NewEfdemodbcontainer (); - db. Orders.add (o); -             intresult =db. SaveChanges (); -  +             varOrders = fromOdinchDb. OrdersSelectod; -  +             foreach(Order Order2inchorders) A             { atConsole.WriteLine ("Orderid:{0},orderdate:{1}", Order2. Id, Order2. OrderDate); -             } -  - Console.read (); -         } -     } in}

Run the result (normally there should be only one row, which is the result of my running multiple times):

Note: If our model changes, we just need to modify the model in the model Design view so that the entity class will change accordingly and then select "Generate from model to database" to re-execute the generated script.

Most of this article is taken from Tri Jiangtao (kenshincui), written by the blog EF Overview of the Entity Framework 5.0 Series

EF three Programming details tutorial (C#+EF), model first

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.