"EF" Modelfirst entity first

Source: Internet
Author: User
Tags scalar

First, preface

In the previous article we introduced Dbfirst, which requires an existing database and then creates an entity from the EF map. Very simple and easy to learn. Here we introduce another kind of--modelfirst, entity first.

Ii. Introduction of the content

Model first uses some tools, such as the EF Designer for VS, to design a visual Entity Data model and the relationships between them, and then generate database objects and related code files based on those entities and relationships.
Through the actual combat drill to better display:

Third, actual combat drills

First open the VS2012 and select the console program:

                                                    图一 选择控制台程序
First step adding entities

Then add the ADO entity model to the project:

                                                   图二 选择ADO实体模型

                                                   图三 选择ADO实体模型

Select the model type: Here we choose the "empty EF designer Model", and some versions are also called "Empty models".

                                                   图四 选择模型类型

After clicking Finish, the generated interface:

                                                    图五 生成的界面

In model Design view, add a new entity:

                                                    图六 添加新实体

                                                    图七 添加新实体

                                                    图八 添加新实体

To add an attribute to an entity, there are three types of attributes: Scalar property, navigation property, complex property. These three properties are described below. Here we use scalar properties, after generating new properties, check the properties, click F4, you can set the content.

                                                    图九 设置属性

Follow the same method and create another entity.

                                                    图十 再创建一个实体

Next add the relationship between the two, "User" and "card" is a one-to-many relationship. Right-click User, click New, Associate.

                                                    图十一 添加关系

Choose the right relationship:

                                                    

After adding a relationship:

                                                    图十三  关系后的
The second step is to build the database from the model

Make sure that a blank database already exists in the database, where I built an empty database with code named Efmodelfirst.

                                                    图十四  空白数据库

The following needs to be built into the database, in the Model Design view blank Place select "Build to Database from model ...":

                                                    图十五 根据模型生成到数据库

I believe you are not unfamiliar with this interface!

                                                    图十六 根据模型生成到数据库

                                                    图十七 选择数据库数据库

                                                    图十八 添加链接字符串

                                                    图十九 选择EF模型

Generate Database Wizard:

                                                    图二十 生成数据库向导

Then click Finish to see the generated SQL script:

                                                    图二十一 生成数据库SQL脚本

Right-click in the blanks and select Execute to generate the database:

                                                    图二十二  执行,生成数据库
The third step to delete and change

Write the following code in the class:

Using system;using system.collections.generic;using system.data;using system.linq;using System.Text;using System.threading.tasks;using system.data.entity;using System.data.entity.validation;namespace EFModelFirst{class  Program{Static void Main(string[] args){            # RegionAdd data//1. Declaration contextModelfirstmodelcontainer DbContext=New Modelfirstmodelcontainer(); 2. Operations on the database, adding data//2.1 instantiating entities, assigning values to EntitiesUser u=New User();u.Id= 1;u.Name= "Ares";u.CreateDate=DateTime. Now; 2.2 Increment//entity attach to ContextDbContext.Userset.Attach(U); Add to DatabaseDbContext.Entry(U). State=entitystate.Added; 3. SaveDbContext.SaveChanges(); #endregion# RegionView database Data//method one, useLinqStatement queryCard C=New Card(); 1.LinqStatementvar Item= from s inch DbContext.Cardset                       Select s; Iterate through the contents of the queryforeach (var cardid in item){Console.WriteLine(The result of the LINQ query ID is: " + Cardid.) ID); }//Method II, useLambdaInquirevar Itemlambda=DbContext.Cardset.Where<Card>(s = = S.id = 2).FirstOrDefault();Console.WriteLine(the lambda query ID result is: " + ITEMLAMBDA.) ID); #endregion}    }}

Execution Result:

Iv. Summary

Through this introduction, we know that the model entity can be used to create a database, very good choice, we hope to bring you help. The next article will introduce you to Codefirst.

"EF" Modelfirst entity 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.