Learning Materials: http://msdn.microsoft.com/zh-cn/data/jj193542
The console application created by the video. I will use MVC here.
I. Very magnetic and elegant
I really like watching this person's video. Although I don't know much about it, I don't know what it is. His voice is very magnetic. It makes me feel bored.
Have you seen it? The code is displayed smoothly on the screen, allowing you to understand what elegance is.
I have seen it before, but I still have no way to do it. So I should not only learn the code, but also the process, what to do first, what to do later, and why to do so.
2. Create an MVC Project
Environment: vs. net2013 sql2008 ef6.1 mvc5
Select an individual user account for identity authentication, which saves the trouble of adding and verifying JavaScript by yourself. In actual development, no authentication is used.
3. Create a model
Just as you used to create a database, you can create a model first.
Because personal authentication is used, the system automatically generates some Controler/Model/View.
The code is copied by me, which is described as follows:
1. BlogId can be written in the same way as the foreign key column name. It feels better.
2. The class name uses Blog rather than Blogs. Objects are generally singular. When a data table is generated, it is automatically added to the plural table name Blogs.
3. The Post class has not yet been created, so an error is reported. But now we use Code First, which can be written like this, because we do not compile it now. When writing an object class, even if there is no design document, there should be a box in mind, which entities should be created and what associations are between entities.
4. The Post class uses the singular form, while the set uses the plural Posts with the same name.
5. virtual is used for delayed loading, that is, when you use a Blog object, if you do not use the data in the associated Post table, the system will not load it. When you use it, to query the corresponding Post data in the database.
In this way, you may need to access the database twice or multiple times. Therefore, you do not need to use delayed loading. Instead, you need to retrieve the required fields at one time as needed.
Because it is CodeFirst, when writing an object attribute, you should also add the following common features:
Enter [] in Name and Display in it, and I will spell the word, so every time I use her
Right-click Display and import the namespace.
You can also press Ctrl +. after entering the Display, press Enter?
Make it as follows:
The length (20) is still a bit useful. Otherwise, if you accidentally enter 100 characters, it is not only unnecessary, but you also need to consider the list display problem.
4. Create a context
The above only creates two entity classes. to associate with a database, you also need to create a context.
5. Install (update) EF, which must be updated online
If there is no package management console at the bottom of your VS interface, open it as follows:
6. edit web. config and add the connection string
What should I do next? Let's take a look at the video.
VII. Generate the interface
Because it is a brand new database, you can directly write the interface after creating an object and associating it with the context.
Compile it first.
8. Delete the original database
At this time, the program is not running, so no database is generated.
Delete the original one first. Is it brand new? How should it be.
Really honest, clean
IX. Running List page
Empty
10. Add records
11. view the database
I didn't use it because I couldn't connect.
12. model changes
1,Enable-Migrations
2. Modify Object
3,Add-Migration AddUrl
4,Update-Database
13. Add a new model
14. Add an object to the context
15th,Add-Migration AddUser
16. Update-Database