Learning materials: http://msdn.microsoft.com/zh-cn/data/jj193542
Video built by the console application, I'm using MVC here.
One, very magnetic, very elegant
Very like to see this person's video, although I do not understand, I do not know what his name. His voice is very magnetic, let me ... Let me...... , don't tire of hearing.
Did you see it? As he explains it, the code is displayed smoothly on the screen, letting you know what elegance is.
Originally also seen, but found that hands-off, so, not only should learn the code, should also learn the process, what to do first, what to do, why to do so.
Ii. Building an MVC project
Environment: vs.net2013 sql2008 ef6.1 mvc5
Authentication Select the individual user account, just exempt yourself from adding verification js trouble, the actual development using no authentication.
Third, create the model
Just like you used to create a database first, it's time to create a model.
Because of the use of personal authentication, the system automatically generates some controler/model/view without having to worry about it.
I copied the code, which is described below:
1, BlogId so write can and foreign key column name consistent, feel so good
2, the class name uses is the blog, but is not blogs. The entity is generally singular, and when the data table is generated, it is automatically added to the plural table name Blogs.
3, Post class has not been established, so error. But now we're using code first, which we can write because we don't compile now. When writing an entity class, even if there is no design document, there should be a frame in mind, which entities to build, what is the relationship between entities.
4, the Post class uses the singular form, but the collection uses the same name plural Posts.
5, virtual for lazy loading, that is, when you use the Blog entity object, if you do not use the associated post table data, the system will not load, you use, only to the database to query the corresponding post data.
This may require access to two or more databases, so lazy loading is not typically used, but the required fields are retrieved once as needed.
Because it is a codefirst, when writing entity attributes, you should also add the usual features by the way:
Type [] On the name, enter display inside, and I'll spell the word, so use her every time.
Right-click on display to import the namespace.
You can also do this by pressing CTRL + After you have finished entering display. , and then enter, cool or not?
To do this:
Plus the length (20) is still a bit of use, otherwise if you accidentally entered the 100 characters, not only not necessary, you have to consider the list of problems to display.
Iv. Creating a context
Only two entity classes are created above, and if you want to associate with a database, you need to create a context.
Install (update) EF, need networking update
If you do not have a package management console under the VS interface, open it as follows:
Vi. editing web. config, adding connection strings
What are you doing down there? And look at the video.
Vii. Build Interface
Because it is a new database, it is possible to write the interface directly after the entity object is created and associated with the context.
Compile it first.
Viii. Deleting an existing database
The program is not running, so the database is not generated yet.
First delete the original, brand-new, how the original should have.
It's really clean.
Nine, run the list page
The empty
X. Adding records
11. View the Database
I just didn't use it because I couldn't even.
12. Model Changes
1,enable-migrations
2. Modifying entity objects
3,add-migration addurl
4,update-database
13. Add a new model
14. Adding entities to the context
XV,add-migration AddUser
16, Update-database
Getting Started with Entity Framework (EF) and using Code first in a new database