Entity Framework Quickstart Notes Part IV -ModelFirst

Background: In a previous blog, we made a small Demo, use EF to achieve the additions and deletions to the database change search operation, the so-called learning, we must learn by analogy, so learn about the contents of one or facilitate future the extended learning, this blog we mainly look ModelFisrt, what does this mean? ? Chinese say it is the first to take the entity model, that is, we do not establish the premise database, Entity Framework is written, let EF framework to help us complete the act of establishing a database, here we introduce in detail, and we are still on as a chapter, use the console to operate.

1. Create a new console application named: EFModelFirstDemo

2. Right-add a ADO.NET Entity Data Model in the project

3. Select the model model is empty, as shown in FIG Finish

  

4. When the click is complete, there will be a visual entity data model designed in a way, how the above operation, there are some tips, as shown:

  

5. Add a frame Entity Entity FrameWork

  (1) from the toolbar and drop a new entity or entities to the right designer, newly added to the entity named UserInfo, but we have added to it an entity, the default has given us add an ID primary key. as the picture shows:

      

  Note: The default generated entity type ID is automatically increased, if we do not need or use a Guid ID serial number, we must set the value of None StoreGeneratedPattern right above the ID attribute.

  (2) the right to add three scalar attribute UserName, Pwd, SubTime entity above. Then for each field to scalar property to property, not explained in detail here, as long as we learned database, then it will be set, as shown:

    

  NOTE: If the maximum length of the attribute is not set, then the default Nvarchar (Max), to set the type of SubTime time DateTime type.

  (3) we add a user role Orders table and a table, and then to establish the relationship between these three tables, how to build relationships? In fact very simple, as long as we know the relationship between the two tables is one to one, or many-to-many, and a new right is associated, the entire final model is shown in Figure:

     

6. According to the above model generating database

  (1)在设计器中右键单击根据模型生成数据库,打开如图所示的情形,对这张图大家很熟悉。 

    

  (2)然后对上面的图形进行填写和链接数据,如图所示:注意下图中的选择或输入数据库名称我们要自己输入数据库的名称,然后单击确定,在单击下一步,完成的时候数据库已经帮我们创建好了。

    

  (3)当单击完成之后就会根据实体模型生成SQL语句,如图所示:

    

  (4)然后在上面的SQL语句中德空白中右键单击执行,将会出现登录数据库的界面,这时候我们将登录数据库的信息填写完毕,单击Conntect,即可帮我们创建好我们模型中的表在数据库中。查看数据库如图所示:数据库的创建位置是默认的位置 

    

  (5)当到这一步的时候我们已经完成了创建数据库的工作,是不是很爽呢。下来我们就验证一下我们所生成的数据库的是否能够使用。

  (6)这时候如果我们设计的数据模型不合理或者那里有点小的Bug,怎么办呢?很简单了,再重新生成一把不就行了。

  (7)如果我们在数据库中变化了呢?我们的模型变化了吗?没有,在这里要注意我们的模型必须要和数据库一致。那么如何保持一致呢,在模型上面单击右键选择从数据库更新模型, 出现一个界面,然后我们单击完成,这时候我们的模型就和数据库一致了。

7.使用EF帮助我们实现添加一条数据

  (1) 实现的代码如图所示:

    

  (2) 数据库里面加入了三条信息,如图所示:

    

  (3)使用数据库的Profiler查看生成的SQL语句如图所示,总共执行了三条语句,这里就不一一展示了。

    

8.使用EF帮我们实现一个查询的结果

  (1) 第一种两个表之间相连实现的代码如图所示:

    

  (2)使用数据库的Profiler实现查看上面执行的SQL语句如图所示:

    

  注释:从上面的执行结果可以看出EF框架在执行上段代码的时候使用Inner Join关联两个表之间的关系。

  (4)使用延迟加载来查询UserInfo表的信息,有些信息在代码的注释里面有:代码如下

   

  (5)查看结果如图所示

    

  (6)通过Profiler查看生成的SQL语句如图所示:   

    

    

  注释:如果有任何疑问,可以到我们的博客园小组里面给我留言,我们的小组名称是”青春代码”,我们大家可以共同互相探讨一下,也可以在下面给我留言,这样只能我回复了,为了技术,让我们共同努力吧!

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/12/03/2799122.html

Guess you like

Origin blog.csdn.net/weixin_34324081/article/details/93495298