EF Model First, EFModelFirst
1. Add the ADO. NET object data model (edmx)
1. Add edmx to create a class library project, add a new item to the project, and select the data/ADO. NET object data model, as shown in. Click Add. In the Object Data Model Wizard window, select an empty EF designer model and click Finish. The edmx file is created successfully. 2. Add the entity model. Next, add the entity model to the edmx designer. Right-click the designer to add/entity, or drag an entity from the toolbox.
2. Add T4 template (including DbContext and Entity) 1. Add a new item to the T4 template project and select the data/EF 6.x DbContext generator, as shown in. In this case, two files are added to the project, xxx. Context. tt and xx. tt. One is the DbContext Context template and the other is the entity template. 2. Modifying the newly added T4 template of T4 does not work immediately. You must make relevant settings. This is very important. First, modify $ edmxInputFile $ to the specific edmx file name, including HwaDB. Context. tt and HwaDB. tt, as shown in. Second, modify the content of the DbContext constructor. The default value is as shown in figure before modification. The modified image is shown in. Make two changes: Enable delayed loading to false, and modify the HwaDBContainer constructor in HwaDB. Context. tt. 3. Generate table creation Script 1. Create a database connection in the edmx workspace right-click the menu and generate a database according to the model... to open the data generation wizard window, as shown in. Select New connection to open the Connection Properties window and create a new connection. Click "OK" to return to the database generation wizard window. Click "Next" To Go To The completion page. Click "finish" to generate the SQL script. In this case, an SQL script is automatically generated in the project, such. 2. Generate a table creation script and create a data table. Open the SQL script, click Run, or open and execute the script in SQL SERVER to create a data table.