.NET中微软实体框架的数据访问方法

.NET 同时被 2 个专栏收录
9 篇文章 0 订阅
9 篇文章 0 订阅

介绍

本文的目的是解释微软的实体框架提供的三种数据访问方法。网上有好几篇关于这个话题的好文章,但是我想以一个教程的形式更详细地介绍这个话题,这个教程对于开始学习实体框架及其方法的人来说是个入门。我们将逐步探索每个方法,通过这些方法,我们可以在应用程序中使用实体框架访问数据库和数据。我将使用实体框架版本6.2和.NETFramework 4.6。和Visual Studio 2017的教程。对于数据库,我们将使用SQLServer。如果没有安装SQLServer,则可以使用本地数据库。我将在本文中首先解释数据库和模型优先方法,并在下一篇文章中解释代码优先方法和代码优先迁移。

实体框架

微软实体框架是ORM(对象-关系映射)。维基百科的定义对于ORM和PITY非常简单。

计算机科学中的对象关系映射(ORM、O/RM和O/R映射工具)是一种使用面向对象的编程语言在不兼容类型系统之间转换数据的编程技术。实际上,这将创建一个可以从编程语言中使用的“虚拟对象数据库”。

作为一个ORM,实体框架是由Microsoft提供的数据访问框架,它有助于在应用程序中建立对象和数据结构之间的关系。它是通过传统的ADO.NET构建的,并作为ADO.NET上的包装器。它是对ADO.NET的一种增强,它以更加自动化的方式提供数据访问,从而减少了开发人员与连接、数据读取器或数据集的斗争。它是对所有这些的抽象,对它所做的贡献更为强大。开发人员可以对他所需要的数据、形式和数量有更多的控制权。没有数据库开发背景的开发人员可以利用实体框架和LINQ能力编写优化查询来执行DB操作。SQL或DB查询执行将由后台实体框架处理,它将处理可能发生的所有事务和并发问题。EntityFrameworkTutorial是一个网站,您可以通过它深入了解这个概念,并理解它的工作模型、体系结构等等。

实体框架方法

了解微软实体框架提供的三种方法是非常常见的。三种方法如下:

  1. 模型优先
  2. 数据库优先
  3. 代码优先

从数据模型类生成数据库。

从现有数据库生成数据模型类。

模型第一种方法说,我们有一个模型,该模型具有各种实体和关系/关联,使用该模型,我们可以生成一个数据库,最终将实体和属性转换为数据库表,列和关系将转换为外部分别是密钥。

数据库第一方法表示,我们已经有一个现有的数据库,我们需要在我们的应用程序中访问该数据库。我们可以直接从数据库创建实体数据模型及其关系,只需要几次单击即可开始从我们的代码访问数据库。所有实体,例如,类将由实体框架生成,实体框架可用于应用程序数据访问层以参与DB操作查询。

代码优先方法是EF的推荐方法,尤其是在从头开始开发应用程序时。您可以预先定义POCO类,以及它们的关系,并通过在代码中定义结构来想象数据库结构和数据模型的样子。最后,实体框架将负责为您的POCO类和数据模型生成数据库,并负责事务、历史和迁移。

通过这三种方法,您可以完全控制在任何时间点按需要更新数据库和代码。

模型优先

使用模型第一方法,开发人员可能不需要编写任何代码来生成数据库。实体框架提供了设计器工具,这些工具可以帮助您创建模型,然后从中生成数据库。这些工具更多的是拖放控件,它们只需要输入,比如实体名是什么、应该具有什么属性、与其他实体如何相关等等。用户界面是非常容易使用和有趣的。模型设计器(如果可以的话)将帮助您生成DDL命令,这些命令可以直接从Visual Studio或在您的数据库服务器上执行,以便从您创建的模型创建数据库。这将创建一个EDMX文件,该文件存储概念模型、存储模型和两者之间的映射信息。要了解概念模型和存储模型,可以参考本文介绍中所提供的链接。我能够看到的唯一缺点是,完全删除数据库并重新创建数据库将是这种方法的挑战。

数据库优先

当我们已经有一个现有的数据库并且需要在我们的应用程序中访问时,我们首先使用数据库的方法。使用实体框架为现有数据库建立数据访问方法将帮助我们在解决方案中生成上下文和类,通过这些上下文和类我们可以访问数据库。这是模型第一种方法的反面。这里,模型是通过数据库创建的,我们可以完全控制在模型中包括哪些表,以及包括哪些存储过程、函数或视图。您的应用程序可能是一个子应用程序,它不需要大型数据库的所有表或对象,因此您可以在这里自由地控制应用程序中所需的内容和应用程序中不需要的内容。每当数据库模式改变时,只需单击设计器或实体数据模型即可轻松更新实体数据模型,这将负责映射并在应用程序中创建必要的类。

代码优先

使用代码第一方法,开发人员的重点只在代码上,而不是在数据库或数据模型上。开发人员可以在代码本身中定义类及其映射,由于现在实体框架支持继承,所以更容易定义关系。实体框架负责为您创建或重新创建数据库,不仅如此,在创建数据库时,还可以提供种子数据。例如,当创建数据库时,您希望您的表具有主数据。首先使用代码,您可能没有具有关系和模式的.edmx文件,因为它不依赖于实体框架设计器及其工具,并且由于您是创建类和关系并管理数据库的人,因此将对数据库有更多的控制。出现了代码优先迁移的新概念,这使得代码优先方法更易于使用和遵循,但在本文中,我将不使用迁移,而是使用创建DB上下文和DB集类的旧方法,以便您了解隐藏的内容。代码优先方法还可以用于从现有数据库生成代码,因此它基本上提供了两种可以使用的方法。

行动中的实体框架方法

足够的理论,让我们从执行部分开始,一步一步地一步一步地探索和学习每一种方法。我将使用一个示例项目,并将其用于控制台应用程序,以使用实体框架连接数据库。我会用基本的样本表来解释这个概念。这里的目的是学习概念并实现它,而不是创建一个大的应用程序。When you learn it, you can use the concepts with any large enterprise level application or any big database server which can have thousands of tables. 所以,我们将遵循接吻策略,并在这里保持简单。

模型优先

  1. Create a simple .NET Framework console application by opening your visual studio and choosing the console application template. We can choose any application type like a web application that could be ASP.NET web forms, MVC or Web API or windows application/WPF application. You can give a name to the project and solution of your choice.

  2. We’ll have Program.cs, the only class and App.config in our project.

  3. Right-click the project and click on add a new item, this will open the window to add a new item, just go to Data as shown in below image and choose ADO.NET Entity Data Model as shown in the following image. Give it a name. For example, EFModel and click on Add.

  4. Once you click add, you’ll be shown to choose Model Contents, and this is the place where you choose what approach you want to use for data access out of the three EF approaches. So, choose Empty EF Designer because we would be using model first approach and create a model from scratch.

  5. Once you click "Finish", you see the empty designer window that is the .edmx file. The name of the .edmx file in solution is the name that we provided while adding the EF designer model. In the toolbox, you see the tools available that you could use to create entities and associations between them.

  6. Drag and drop the Entity tool from the toolbox into the designer. It will create an empty entity as shown below with one property named Id, saying it is a primary key. Here you can rename the entity and add more scalar properties.

  7. Right click on entity created and add a new scalar property as shown in the following image. Rename the name of the entity from Entity1 to Student. You can rename the entity by double-clicking on the entity name and right click and rename the entity.

  8. Name the scalar property as "Name".

  9. In an analogous way add a new entity named Class and add a new property named ClassName. We here are trying to create a student and a class relationship where a class can have multiple students. So, we have an option to choose Association from toolbox as shown below and drag the Association tool from Class to Student and it showed 1 to many relationship.

  10. We are not adding more entities and try to understand the basic functionality with these two entities. Right click on the designer and click on "Generate Database from Model…" option to generate the scripts.

  11. Once you click on "Generate Database from Model…" option, you’ll be asked to choose data connection as shown in the following window. You can choose a new connection or an existing one. I’ll choose a new connection but before that, I’ll create an empty database on my SQL server so that I do not have to modify my scripts to provide a database name. By default, the generated scrips create tables in the master database if DB name is not specified.

  12. Open your SQL Server and create a new database and name it as per your choice. I am naming it StudentDB as shown in the following images.



  13. Coming back to the window where we needed to provide the connection details. Choose your data source and server name as shown in the following image. The server name should be the server where you created the empty database. Now in the selecting database option, expand the dropdown and you should see your database name. Select the database name.

  14. Once you select the database name, a connection string would be generated as shown below and it will say that the connection string would be saved I the App.Config file with the name EFModelContainerEFModelContainer is the name of the connection string. Since it is an EF generated connection string, you see it has the information about EF CSDL, MSL and SSDL files as well that would be present in our application. Click Next to proceed.

  15. Next step is to choose your Entity Framework version. We’ll use 6.x i.e. it will automatically pick the latest stable version with EF6. Click Next.

  16. As a last step of the wizard, you’ll see the needed SQL scripts created for us. You can choose to rename the scripts but by default, it takes the name as <model name>.edmx.sql. I’ll leave it as it is and click Finish to proceed.

  17. You’ll see the script located in solution explorer now. Double click to open it and it opens in a window where you have an option to directly execute it.

  18. Before executing the scripts let’s first install Entity Framework latest stable version from the Nuget package manager. It is very simple to do. Go to Tools in Visual Studio, then choose NuGet Package Manager->Package Manager Console as shown in the following image.

  19. The NuGet Package Manager console window will be opened at the bottom of Visual Studio by default. Now choose the project for which the Entity Framework package needs to be installed. And in the command that says PM> type Install-Package EntityFramework and press enter. We do not specify the version of entity framework as we want the latest stable package should be downloaded and added to our project as a DLL reference.

  20. Once done with installing Entity Framework, go back to the script window and on the top left, you see the button to execute the scripts as shown below. Press the button to execute the scripts.

  21. Once you click on Execute, a new window will show up asking server and database details. Fill in the details specific to your server and database as shown below and click Connect.

  22. Once done, go to your database server and you’ll see the tables are created for our database StudentDB. The names of the tables are pluralized, and Student table has a foreign key reference to Classes table and the foreign key is automatically created named Class_Id referencing Classes table. It is magical, isn’t it?

  23. In our solution explorer, we see the .edmx file and the context classes created and model classes for Student and Class entities. This is all done in the background by EF designer. So, till now we did not write a single code and got all the code generated by EF.

  24. Open the EFModel.Context.cs class file and we see the name of the DbContext class that got generated is EFModelContainer. Remember that is the name of our connection string stored in App.Config. The name of the context class has to be the same as connection string name for EF to know the relation. So, you can have multiple DB context classes in the same solution with different names and pointing to different connection strings. You can explore more on DbContext class and what other ways you can make its relation to the connection string in the config file. One another way is to call its base constructor by passing name of the connection string as a parameter to its parameterized constructor. But for the sake of understanding, we’ll stick to this implementation.

  25. Now it’s time to test our implementation and check if the entity framework is actually working and helping us in database operations or not. So, in the Program.cs class’s Main method we’ll try to write some code that saves a new class for us in the database. Create a new object of EFModelContainer and in the container, we get the entity classes collection coming from DbContext. Add a new Class. The class is the name of the entity class generated for us via designer. And name the class as "Nursery". We do not have to specify the id attribute for the class, as EF will automatically handle this and provide an Id to a newly added record. The code to add a new class named "Nursery" is shown in the following image. The container.SaveChanges statement is the statement when executed will add a new record in the database for us.

  26. Just run the application and let the main method code execute. Once done, go to your database and check the Classes table, you’ll see a new record is added in the Classes table with the class name "Nursery" which is what we provided while wanted to add a record. So, it works ?. Notice the Id that is auto generated by entity framework.

  27. Now, let’s try something new and try to add a new class but this time with students. We have a relationship of class with students that is a class can have many students and a student will belong to one class. Check the created model classes for Student and Class if you want to explore how the relationship is maintained in the classes. So, this time, we’ll add a new class and add some students to that class. Entity Framework should automatically add these students to the Students table and make the relationship with the Class table. Following is the simple self-explanatory code for doing this.
    static void Main(string[] args)
    {
        EFModelContainer container = new EFModelContainer();
        ICollection<Student> students = new List<Student>
        {
            new Student() { Name = "Mark" },
            new Student() { Name = "Joe" },
            new Student() { Name = "Allen" }
        };
    
        container.Classes.Add(new Class() {ClassName = "KG", Students = students });
        container.SaveChanges();
    }
    

    In the above code, we create an EFModelContainer object and a list of Students by adding three students into it. Now add a new class to the container object, just like we did in the last example and assign students to the Students property of the Class object. Last but not the least, container.SaveChanges().

  28. Run the code and go to the database. Check the Classes table and see a newly created class row with name "KG" that we supplied from the code.

Now, go to the Students table and we got three students created there those we supplied from code and check the Class_Id column that has the foreign key reference to the newly created class with Id 2. Amazing ?

Like this, you can perform complex queries and other CRUD operations on your database by writing simple code. Try to perform more operations like editing, deleting fetching the records to understand more. Let’s move to our next topic that is database first approach with Entity Framework.

Database First

  1. Like we did in model first approach, create a new console application and name it EF_DBF.

  2. The second step is to add a new ADO.NET Entity Data Model to this project. Name it of your choice. I named it ModelDBF.

  3. Now, from the choose model window, we’ll choose the option of EFDesigner from the database, this will help us to create an entity framework designer from the existing database.

  4. Next, choose the connection for the database. For example, provide the details on the wizard for your existing database. I’ll take the database we created with our model first approach i.e. StudentDB. Once we choose the database, we see the entity framework connection string and the name of the connection string to be saved in App.Config i.e. StudentDBEntities. You can also change it if you want. Click Next.

  5. Choose the EF version. I already explained the meaning of 6.x. We’ll choose the same and click Next.

  6. Now in this step, you would be shown all the database objects related to the database you selected initially, and it is your choice to include or exclude the objects you need. The objects could be tables, views or stored procedures. Since we do not have views and stored procedures, we’ll only choose our two tables as shown in the following image. Since we already have the table’s name in pluralized forms, I do not want to complicate this by again pluralizing it and appending one more ‘s’ to my entity classes, so I unchecked that option of pluralizing the entity names. Provide model namespace or leave it as it is with the default name provided and click Finish.

  7. Once you click Finish, you see the entities created in the EF designer for the database objects we selected from the database. We notice that it is like what we had when we manually created the entities and generated a database out of it. This EF designer also takes care of foreign key relationship and shows the one to many association between class and student entities.

  8. Time to add Entity Framework package like we did in the first approach discussed. Make sure you choose the right project, for example,the current project where you need to add EF. Type the command in package manager Console and press enter.

  9. Now, when we open the generated ModelDBF.Context.cs, we see the name of the partial class as StudentDBEntities i.e. the name of the connection string we got stored in App.Config. I have already explained the logic behind it in the last section.

  10. Time to see some action now. Add the code below to the Program.cs Main() method.
    static void Main(string[] args)
     {
         StudentDBEntities container = new StudentDBEntities();
         ICollection<Students> students = new List<Students>
         {
             new Students() { Name = "Harry" },
             new Students() { Name = "Jane" },
             new Students() { Name = "Nick" }
         };
    
         container.Classes.Add(new Classes() { ClassName = "Class 1", Students = students });
         container.SaveChanges();
    
         container.Students.Add(new Students() {Class_Id = 1, Name = "Ben"});
         container.SaveChanges();
    
     }
    

    In the above code, we are trying to create an object of StudentDBEntities class. For example, our context class and a collection of students to be added to our database. To check the relationship is working fine or not, we’ll add a new class named "Class 1" and assign the Students property to our students collection and SaveChanges() again to check if individual student insertion is working or not, we’ll add a new student named "Ben" to the Students model and assign the class id to 1. For example, the existing class we have in database and SaveChanges(). Put a breakpoint in Main method and press F5.

  11. When the application runs it will hit the breakpoint. Navigate through the statements by pressing F10 and stop at line 24 i.e. before we add a new student. Since we already executed the code for saving changes for newly added class. Let’s go to the database and check.

  12. In the database, we see the newly added class has a new row in Classes table with ID 3.

    And in the Students table, we see that three students that we added from code got inserted in the table with the class id as 3 i.e. the newly created class.

  13. Now get back to Visual Studio and execute the line for adding a new student.

完成后,检查数据库,我们看到一个新学生,名字是“Ben”,添加到我们的学生表中,具有Class_Id 1,我们在代码中分配。

我们看到我们的数据库第一种方法也很好用。再次,您可以尝试在您的代码中的其他DB操作,并与代码玩更多的探索。让我们转到代码第一方法。

结论

  在本文中,我们仔细研究了如何利用实体框架方法,并根据需要使用这些框架。我使用基本的控制台应用程序来解释这些概念,但是这些概念可以用于任何使用WebAPI、ASP.NET项目或MVC项目的企业级应用程序中。我们简要地讨论了使用的方法的优点和缺点,并尝试创建小样本应用程序,以查看那些工作。实体框架有很多需要探索的地方。例如,底层架构是什么,架构如何工作,事务管理,加载等等。我个人认为EF是最好和最强大的ORM之一,可以与任何.NET应用程序无缝集成。在本文中,我故意跳过代码第一方法和代码第一迁移,因为这会使文章冗长。在下一篇文章中,我将解释在实体框架中使用实体框架和代码第一迁移的代码第一方法。

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:创作都市 设计师:CSDN官方博客 返回首页

打赏作者

qq_43235676

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值