ASP.NET MVC - CodeFirst development model

   Entity Framework development framework offers several modes, such as Database First, Model First, Code First. Database First is the oldest and most widely applied a design approach. Database First designed in this way is highly dependent on the structure of the database table to create a model based on relationships between tables and tables. If the latter are subject to change or functional requirements vary widely, then, need to involve the cost of changes to the database will be a great pay, because before writing good code will no longer apply to the new table, we need to change the code refactoring logic in order to adapt to the table after the change. ADO.NET Entity Model First is to create objects and the relationships between them, and then specify the mapping database. The entity object is the Model.

       Our story today is Code First (Code First). It is thought that the first model in the class definition, and then generate the database through these classes. This mode is suitable for the development of new projects, it allows us to design rather than the first as the core database structure to the code, follow these steps.

       1. Then I will take a simple example to introduce this development model. Our needs are two tables, tables and blog comment form. A corresponding number of blog comments, a comment corresponding to a blog. This is a to-many relationship. Let's create a new ASP.NET MVC project, and install EntityFramework with NuGet. Then create two Model.

public  class Blog

{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public DateTime Time { get; set; }
    public string Summary { get; set; }
    public string Content { get; set; }
 
    public virtual ICollection<Comment> Comments { get; set; } = new List<Comment>();
}
public  class How
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public string VisitorName { get; set; }
        public string Email { get; set; }
        public DateTime Time { get; set; }
        public string Content { get; set; }

        [ForeignKey("Blog")]
        public Guid BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

 2. Next we create a data context. Create a new folder called Context, and in which the new class, as follows:

 public class TestDbContext: DbContext
 {
        public TestDbContext()
          : base("name=ConString")
        {

        }
        #region database related tables in this new table needs to add the corresponding relationship

        public virtual DbSet<Blog> Blog{ get; set; }
        public virtual DbSet<Comment>Comment{ get; set; } 
        #endregion 
 }

3. Then we configured in Web.config database, in Web.config <configuration> node was added as follows (Note: connectionStrings position and the node section)

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework"
      type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add  name="ConString"  connectionString="Server=.;DataBase=test;Uid=sa;Pwd=123456" providerName="System.Data.SqlClient" />
  </connectionStrings>

4. After the addition was complete, data migration

Open the Package Manager Console (Package Manager Console)

Input commands 1, enable-migrations carriage. 2, add-migration carriage return, enter the name of easy understanding. 3. Here we perform a formal migration, enter the update-database Enter, get.

Guess you like

Origin www.cnblogs.com/pengchong/p/12349563.html