In the previous < MVC rental site (ii)-Framework construction and preparation >, the Project Framework was set up and EF, log4net, and out-of-process sessions were configured. Next, you will write the Eneity class and create the database in Codefirst way.
Example of database table structure
1. before writing the entity class in the Codefirst manner, it is still necessary to understand the table structure of the database first. First of all, there will be users, administrators, roles, permissions and other related tables, and then for the special scenario of renting, as well as housing, home appliances configuration, the address of the house, including cities and regions.
2. There are two tables that are more special, t_settings and T_idnames:
The t_settings is used to store configuration items that are required to run, which were previously stored in Web. config.
T_idnames is used to centrally save some similar key-value pairs of options, such as the House apartment will have a room and a hall, a room and two halls, decorate the state will have hardcover, lite, blank, such as the state, this kind of options if all alone to build a table, the number of tables will be more, all concentrated into the t_idnames.
3. There are various associations between the table and the table, such as the Association of the user, the role, the authority, the house and the decoration status, the house and the housing configuration, etc.
Second, create the entity class
1. Once the data table structure has been clarified, the entiy can be created according to the table structure:
There is a special baseentity that does not correspond to a table in the database, but rather extracts some common properties of all the tables, such as Id,name.
And, here is the default assignment of attributes, this is a newer syntax, last time?? is 7.0 of the syntax is questioned, this property to assign the default value of the wording is not sure that the version.
To use virtual adornments when referencing between entities, permissionentity refers to roleentity, because it is possible to use the lazy loading of EF
2. Configuring entities with Fluentapi
There are two ways to configure an entity, one is to add a label
For example, the maximum length for setting the MSG attribute is 50;
Another is the FLUENTAPI that will be used, which facilitates centralized configuration and is more popular.
The configuration for permissionentity is as follows:
? This will be configured to set the name of the table, the maximum length of the field, whether it is nullable, and so on, as the data table is generated.
third, build the database
Write the configuration for each entity, run the project, and the database will be created automatically. By default, EF keeps track of and automatically maintains changes in the correspondence between the entity and the data table (through dbo.) __migrationhistory), there is no need to do this, turn off this feature by setting database.setinitializer<zszdbcontext> (NULL) in the DbContext constructor.
Note:
The content of the course to free Peng Network (www.rupeng.com), focusing on college students employment online education platform;
ASP. NET MVC course http://www.rupeng.com/News/9/640.shtml
Learn the MVC rental website (iii)-write the entity class and create the database