Entityframework learning notes -- 002-several methods for creating models, entityframework
1. the Entity Framework is tightly integrated with Visual Studio. To use the Entity Framework in your application, we add an ADO. NET Entity Data framework to your project. The method is as follows:
Right-click your project and select New Item ). In the pop-up dialog box (1-4), select the ADO. NET Entity Date Model (Entity Data Model) Template under the Data (Data) template. Click Add to open the data model creation guide.
Figure 1-4 Add an object data model to a project
2. in the pop-up dialog box, there are four options: 1 for the EF designer from the database, 2 for the EF designer model, 3 for the CodeFirst model, and 4 for the Code First from the database. Among them, 1 and 2 are regarded as a way to design the entity model using a visual ef designer; 3 and 4 are considered as a way to design the entity model using code. For example, 1-5. In this example, the table structure of the database I used is 1-5-2.
Figure 1-5
Figure 1-5-2
3. among them, the most used by beginners is the first type of "EF designer from the Database". Let's select the first type and click "Next ", in the displayed dialog box, select "New Connection" and create a new database connection. 1-6.
Figure 1-6
4. Select "Yes, include sensitive data in the connection string" in the pop-up dialog box and click "Next ". 1-7
Figure 1-7
5. select the required database and table and select "determine the form of single and multiple numbers" (this is to generate an entity to remove the form of plural numbers. For example, if our table is called Students, the generated entity should be called Student, it is not appropriate to call it Students .), Click "finish ". 1-8
Figure 1-8
6. the generated ef object model view is shown in 1-9. We can see that the relationships between tables have been mapped. Therefore, the ef designer also generates navigation attributes for us, you can easily access the associated attributes through navigation attributes.
Figure 1-9
7. The following is a simple code example. We can use the Model Designer's "myefrecipesEntities" data category for database access. "MyefrecipesEntities" is inherited from "DbContext", which is the core of ef data operations.
static void Main(string[] args) { using (myefrecipesEntities db = new myefrecipesEntities()) { Class c1 = new Class() { Name = "class1" }; Student s1 = new Student() { Name = "rj", Age = 18, Class = c1 }; db.Students.Add(s1); int i = db.SaveChanges(); foreach (var item in db.Students) { Console.WriteLine("name \t age \t class"); Console.WriteLine("{0} \t {1} \t {2}",item.Name,item.Age,item.Class.Name); } } Console.ReadKey(); }View Code
Code output:
8. Let's take a look at this article. There are several ways to generate an entity model. In short, no matter whether you use database first, model first, and code first, all the objects to be operated are the inner entities, such as Student and Class in this example. The entity can be the same as other types in the program, which greatly provides convenience and improves the sending efficiency.