ASP. NET MVC 4.0 Learning 4-code First

Source: Internet
Author: User

The numbers that we need to use before, through adding an entity physical model, pull the database needed in the library into the project as follows,

And that's code first, which is relative to the way this is handled.

code First is more accurate, and the developers only need to write the program (code only), and the system will automatically build the model and the library.

Let's create a new case and look at code First's reality.

1, the model of the new case added category messageboard Storage message information

To add a field in MessageBoard.cs:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel;namespacemvcapplication3.models{ Public classmessageboard {[Key] Public intMsgID {Get;Set; }        [Required] [DisplayName ("Name:")]         Public stringTitle {Get;Set; }        [Required] [DisplayName ("content")]         Public stringContent {Get;Set; } }}
View Code

After the 2,ctrl+shift+b reconstruction scheme, add Messagecontroller

Select the function to read and select the messageboard in the model you just created.

After confirming, we see that the models and views in the self-generated the corresponding file, MessageController.cs and view in the self-added changes to the change:

Ctrl+f5 Run the program, enter a message such as:http://localhost:64570/Message We see the first page of the message, And can add messages

The function of the add-and-change check is that we just added the "have read/write in ..." function when adding a controller:

→→

We just didn't build the repository, where did the new messages go?

Click to view all the files, we found that the app_data below produced the. mdf file, the. mdf file, and the MvcApplication3Context.cs under models are also added in our Messagecontroller This step selects the "Material content category" When it is produced:

→→→→

Double attack. MDF we saw that the resulting table and models under MessageBoard.cs are consistent:

This is code first, and we just need to compile the model in models and add the controller to help us generate a consistent repository file

And then we'll look at the model in models:

The class response in the Code First,model model generates the Table:MessageBoard.cs Response messageboards table in the repository.

The class in model can also define the name of the table in addition to its definition, the main key in table and the one-to-many relationship between the table:

The model adds two models, BookModel.cs and AuthorModel.cs, and the ICollection of the system.collenctons named space below the table is the relationship between tables and tables:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;namespacemvcapplication3.models{//Definition Table name[Table ("MyTable")]     Public classBookmodels {//definition key keys[Key] Public intID {Get;Set; }  Public stringBookName {Get;Set; }  PublicDateTime Publishtime {Get;Set; } //Book and author's relationship: N*1         PublicAuthormodels Authormodels {Get;Set; } }}
View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;namespacemvcapplication3.models{ Public classAuthormodels {//Definition Table name[Table ("Bookauthor")]         Public classAuthor {[Key] Public intID {Get;Set; }  Public stringName {Get;Set; } //the author and the number of the relationship: a writer 1* more             PublicIcollection<bookmodels> Bookmodels {Get;Set; } }    }}
View Code

ASP. NET MVC 4.0 Learning 4-code First

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.