Preface
Here, I would like to thank the younger brother. I can continue reading the previous articles, but the next few articles are just a bit of a cloud. Learning is on the one hand, and practice is on the other hand, more things can be learned in the process of doing so. Here we also try to write a small example about Code First.
Create a project
When you install the EntityFramework extension, an error is reported that the installation cannot be completed at the end of the project creation. You are advised to select version 4.0 or later.
Installation Package
After the project is installed, choose project-management package-Online-search "" to download and install the package.
Reference:
System.Data.Entity;
Create model
Several models are created in. cs, which can be separated normally.
NewId { ; Title { ; NewTypeId { ; NewType NewType { ; NewTypeId { ; Name { ; BlogId { ; List<New> New { ; }
System.ComponentModel.DataAnnotations;
UserName { ; ; }
Keyword, indicating delayed loading. My understanding is as follows: When I access the primary object, I start delayed loading instead of querying the database's child, the object is queried and loaded in the database only when it is accessed. The generic List indicates that the object is a one-to-many relationship.
db.Configuration.LazyLoadingEnabled = ;,
Create context DbContext
DbSet<New> News { ; DbSet<NewType> NewTypes { ; }
DbSet indicates the set of all objects of the given type in the context or the set of all objects of the given type that can be queried from the database. It is like a package, if you need something, install something in it.
Create a database and read/write data
( db = Console.Write( name = type_Model = NewType { Name = Console.WriteLine( search_type = query = b b.Name == Console.WriteLine( ( item }
Configure the connection string
Code First migration
[MaxLength( Name { ; ; }
Enter"Enable-Migrations"Command to enable migration,
- Configuration. cs-This file contains "Migration" which will be used to migrate BloggingContext settings. No changes are required in this walkthrough. However, you can specify the seed data, register the provider for other databases, and change the generated namespace for migration.
- <Timestamp> _ InitialCreate. cs-This is the first migration, which indicates that the changes have been applied to the database. The purpose of an application change is to migrate it from an empty database to a database containing blog and article tables. Although we asked Code First to automatically create these tables, now we select "migrate" (converted to a "migrate "). Code First also records in the local database that the "Migration" has been applied. The timestamp in the file name is used for sorting.
Enter"Add-Migration Update-NewType-Name"Command to add a migration, followed by the Migration name, which can be written at will. After running, the system will automatically detect changes to the Model and database, in the Migrations folder, a "201403290930423_Update-NewType-Name.cs" file is generated. open the file and we can see the updated content:
AlterColumn(, , c => c.String(maxLength: AlterColumn(, , c => }
Enter"Update-Database"Command to migrate all the applications to the database and open the database. Let's take a look at the effect:
In addition to the field type modification mentioned above, there is still a lot of content, such as adding a field, deleting a field, deleting a table, and so on. However, they are all similar and can be used in the same way.
Sample Demo download
Postscript