ASP. net mvc 4.0 learning 4-Code First, mvc4-code

Source: Internet
Author: User

ASP. net mvc 4.0 learning 4-Code First, mvc4-code

The data volume we used previously, by adding the Entity actual data volume model to pull the Database required in the data volume to the following example,

The Code First method is similar to this method for processing data.

Code First is more accurate. Developers Only need to write Code Only to automatically create models and data volumes.

Let's create a new example. Let's take a look at the practices of Code First.

1. Add a similar MessageBoard to the Model of the new upload case to store the Upload message.

Added the field Atomicity in MessageBoard. cs:

Using System; using System. collections. generic; using System. linq; using System. web; using System. componentModel. dataAnnotations; using System. componentModel; namespace MvcApplication3.Models {public class MessageBoard {[Key] public int MsgID {get; set;} [Required] [DisplayName ("name:")] public string Title {get; set ;}[ Required] [DisplayName ("Content")] public string Content {get; set ;}}}View Code

2. Add MessageController after Ctrl + Shift + B re-build the Scheme

Optional. The MessageBoard function in the Model created by the Model is not selected.

After confirmation, we can see that the corresponding files are automatically generated by Models and Views. The MessageController. cs and View automatically add the corresponding increment and increment values to the changes:

Ctrl + F5 indicates the running schedule, and the following Message is entered into the worker:Http: // localhost: 64570/MessageWe see the first Message and can add a Message.

When adding a controller, we chose the "have access/pull in..." function to automatically query and modify the added controllers:

We didn't set up a data volume just now. Where is the new message stored?

Click Upload to view all the files. We found that the App_Data file was generated. mdf file ,. mdf file and MvcApplication3Context under Models. cs was also generated when we added MessageController in this step to select "resource content type difference:

Partition layout. mdf we can see that the generated Table is consistent with the consistency in MessageBoard. cs in Models:

 

This is Code First. We only need to modify the model in Models. When adding the Controller, the system automatically generates a data volume file consistent with the model.

 

Next, let's take a look at the model in Models:

Code First, class pairs in the Model data Model generate the Table: MessageBoard. cs pair MessageBoards Table in the dataset.

In addition to defining semantics, the Class in the Model can also define the Table name, the primary partition in the Table, and one-to-many relationship between the tables:

Two Model types are added to the Model: BookModel. cs and AuthorModel. cs. The Icollection class that uses the System. Collenctons namespace to implement the table and table

Response:

1. Update MessageBoard. cs and add the model definitions of Book and Author:

Using System; using System. collections. generic; using System. linq; using System. web; using System. componentModel. dataAnnotations; using System. componentModel; using System. componentModel. dataAnnotations. schema; namespace MvcApplication3.Models {public class MessageBoard {[Key] public int MsgID {get; set;} [Required] [DisplayName ("name:")] public string Title {get; set;} [Required] [DisplayName ("Content")] public string Content {get; set ;}// define the Table name limit [Table ("MyTable")] public class BookModels {// specify the primary Key [Key] [databasegeneratedattrition (DatabaseGeneratedOption. identity)] public int BookID {get; set;} [Required] public string BookName {get; set;} [Required] public DateTime PublishTime {get; set ;} // Book and Author's response: N * 1 public AuthorModels {get; set ;}// define the Table name limit [Table ("Author")] public class AuthorModels {[Key] [databasegeneratedattrition (DatabaseGeneratedOption. identity)] public int AuthorID {get; set;} [Required] public string Name {get; set;} // response of the author and number: one author applies to more than one [Required] public ICollection <BookModels> BookModels {get; set ;}}}View Code

2. Update the MvcApplication3Context. cs file:

Using System. Data. Entity; namespace MvcApplication3.Models {public class MvcApplication3Context: DbContext {// you can add the existing program to this example. Changes will not be overwritten. //// If you want Entity Framework to update the model structure description every time you delete it and then generate a new resource, add the following // program to Global. in the Application_Start method of the asax slave case. // Note: this will change every time the model changes and recreate your data. /// System. data. entity. database. setInitializer (new System. data. entity. dropCreateDatabaseIfModelChanges <MvcApplication3.Models. mvcApplication3Context> (); public MvcApplication3Context (): base ("name = MvcApplication3Context") {} public DbSet <MessageBoard> MessageBoards {get; set ;} public DbSet <BookModels> BookModels {get; set;} public DbSet <AuthorModels> AuthorModels {get; set ;}}}View Code

3. Open the kit manager to control the volume and gradually execute the following command:

The following Code First Migration function can be used as an example:Http://msdn.microsoft.com/en-us/data/jj193542

Success:

  

We can see the newly added Table in the Data Partition:

  

 


All aspnet mvc3 projects are developed using codefirst, right?

Your answer is basically correct, but there is always an accident. I am currently working on the MVC3 project, so this is not the case.

In fact, you are a method of development. Your favorite and suitable projects are the best. Of course, the premise is to meet the needs of the project, rather than simply relying on your own interests.

Hey. It's off duty ,. Eat now.

I have a business website developed by ASPnet40 and MVC20. How can I run it on my XP iis?

Asp.net does not have much difference between IIS settings and asp. The key is that asp must be selected for the website support framework. net4.0 (If yes. net framerk4.0 will be set to 4.0 by default). After creating a virtual directory, right-click the virtual directory and select asp.net as the attribute. Set it to 4.0.

Related Article

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.