Try Entity Framework ModelFirst

Source: Internet
Author: User

Model First is called "Model First". The Model here refers to "ADO. NET Entity Framework Data Model ". At this time, your application does not design related databases. in Visual Studio, we generate databases and Data classes by designing the Data Model.

Step 1: First add a simple console application, and then add an ADO. NET object data model to this project.

Step 2: select an empty model.

Step 3: Add a new object to the model design view

Click OK.

Step 4: Add two attributes to the Order object. The Customer type is string, and the OrderDate type is DateTime.

Set the field type, right-click the current field ==>>> property, and find the type setting.

Step 5: add another entity OrderDetail order details, Product

Step 6: add the relationship between the two. "Order" and "OrderDetail" are one-to-many relationships. "Order" can access the "OrderDetail" entity through the "OrderDetails" attribute, "OrderDetail" can access the "Order" entity through the "Order" attribute and add a foreign key constraint to "OrderDetail ".

Click OK. The added link is

So far, the Model in Model First has been created and needs to be generated to the database. In the Model design view, select "generate to database according to Model ..." :

Make sure that a blank database already exists in the database.

Step 7: select and add a data link

Click Next and you will see the following interface

Click Finish to view the generated SQL script.

Then, right-click the SQL script and click Execute

The database has two more tables.

 

    class Program    {        static void Main(string[] args)        {            using (var db = new ModelTestContainer())            {                Order Order = new Order();                Order.Customer = "aehyok";                Order.OrderDate = DateTime.Now;                db.Orders.Add(Order);                db.SaveChanges();                IQueryable<Order> Orders = from Orderes in db.Orders                                           select Orderes;                foreach (Order O in Orders)                {                    Console.WriteLine("OrderID is {0},Customer is {1}", O.ID, O.Customer);                }            }            Console.ReadLine();        }    }

Running effect:

After running twice, add two pieces of data.

Note:: If our model changes, you only need to modify the model in the model design view and save the changes to the object class, then select "from model generation to Database" to re-execute the generated script.

 

Related Article

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.