ArticleDirectory
- 1. Create a project
- 2. Compile entity classes
- 3. Add efcodefirst
- 4. Configuration
- 5. Test the knife
- 6. After the model is modified, the data table is automatically updated.
- 7. Write at the end
- Source code download
1. Create a project
Open vs2010, select File> New> Project, and create an ASP. NET mvc3 web application.ProgramHere, I name it blog.
2. Compile entity classes
For a blog, several classes should be required:
- Post blog articles
- Comment comments and post are one-to-many relationships
- CATEGORY Directory class, which has one-to-many relationship with post
- Tag tag class, which is a many-to-many relationship with post
- Friendlink links
Do not consider anything like the administrator. Add the above classes in sequence in the model.
namespace blog. models {public class post {public int ID {Get; set;} public int categoryid {Get; set;} Public String title {Get; set;} Public String summary {Get; set;} Public String alias {Get; set;} Public String content {Get; set;} public datetime createtime {Get; set;} public category {Get; set ;} public icollection
tags {Get; set;} public icollection
coments {Get; Set ;}}
namespace blog. models {public class comment {public int ID {Get; set;} public int postid {Get; set;} public int level {Get; set;} public int replyto {Get; set;} Public String username {Get; set;} Public String email {Get; set;} Public String website {Get; set;} Public String content {Get; set ;} public datetime createtime {Get; Set ;}}
namespace blog. models {public class category {public int ID {Get; set;} public string name {Get; set;} Public String alias {Get; set;} Public String description {Get; set ;}public datetime createtime {Get; Set ;}public icollection
posts {Get; Set ;}}
Namespace blog. models {public class tag {public int ID {Get; set;} public string name {Get; set;} Public String alias {Get; set;} public datetime createtime {Get; set;} public icollection <post> posts {Get; Set ;}}}
Namespace blog. models {public class friendlink {public int ID {Get; set;} public string name {Get; set;} Public String URL {Get; set;} Public String description {Get; set ;}public datetime createtime {Get; Set ;}}}
3. Add efcodefirst
Choose tools> library package magager> Package Manager Console
On the Package Manager Console, enter the following command to install efcodefirst:
PM> install-package efcodefirst
After the installation is successful, vs automatically adds a reference to the entityframework in your project.
4. Configuration
The configuration of efcodefirst is quite simple. We add the blogdb class to the model.
Using system. data. entity; namespace blog. models {public class blogdb: dbcontext {public dbset <post> posts {Get; set;} public dbset <tag> tags {Get; set ;} public dbset <Category> categories {Get; set;} public dbset <comment> comments {Get; set;} public dbset <friendlink> friendlinks {Get; Set ;}}}
Open the Web. config file and add a link string
<Connectionstrings> <Add name = "blogdb" connectionstring = "Server =. \; database = blog; trusted_connection = true" providername = "system. Data. sqlclient"/> <! -- <Add name = "blogdb" connectionstring = "Server =. \ express; database = blog; trusted_connection = true "providername =" system. data. sqlclient "/> --> </connectionstrings>
Note that the value of the name attribute is "blogdb", which is consistent with the Class Name of the blogdb class. The database name is blog (this database does not exist now ).
5. Test the knife
Create a homecontroller and add the followingCode.
Using blog. models; namespace blog. controllers {public class homecontroller: controller {blogdb _ DB = new blogdb (); // get:/home/Public actionresult index () {var posts = _ dB. posts; return view (posts );}}}
Create a view for index action, as shown in:
After adding it, you can't wait to press F5 decisively. Let's see what happened!
The following information is displayed on the webpage, but this is not the focus of today. Today, the focus is on databases. Let's open the database and see what happened in it.
Look, EF automatically creates a database for us.
In addition, EF is smart enough to complete the many-to-many relationship between posts and tags !!! In our program, there is no model corresponding to the tagposts table, and some are just the following two lines of code:
In the post class
Public icollection <tag> tags {Get; set ;}
In the tag class
Public icollection <post> posts {Get; set ;}
We can use the following code to obtain all the articles in the label "CSHARP.
VaR posts = _ dB. tags. Where (t => T. Name = "CSHARP"). Single (). posts;
6. After the model is modified, the data table is automatically updated.
When we modify the model and run the website, an error is reported because EF cannot match the updated model with the old data table. In order to make the database updated with the model, we need to do the following.
Open the global. asax file in the root directory
Add the following namespace (Note: efcodefirst 1.0 and 0.8 have different namespaces for the database class)
Using system. Data. entity; using blog. models;
Create a new blogdbinitializer class to inherit dropcreatedatabaseifmodelchanges <blogdb> and rewrite the seed function.
Public class blogdbinitializer: dropcreatedatabaseifmodelchanges <blogdb> {protected override void seed (blogdb context) {base. seed (context); var links = new list <friendlink> {New friendlink {name = "ninofocus.com", url = @ "http://ninofocus.com", description = "ninofocus's blog "}, new friendlink {name = "ninofocus at cnblogs", url = @ "http://www.cnblogs.com/nizhuguo", description = "ninofocus blog"}; links. foreach (L => context. friendlinks. add (l); context. savechanges ();}}
Add the following code to application_start ():
After each database reconstruction, the data in the database is cleared. The role of the seed () function is to add the following initialization data to the new database.
I added two links to the code above.
7. Write at the end
I have just learned the EF framework, and I haven't noticed it in many places, or I am wrong. Please give me some advice!
Source code download
Blog.rar
Tags: efcodefirst, entityframework, ASP. net mvc, DDD