|
1 |
| -# Step 1 - Employee DB |
| 1 | +# Step 2 - Employee DB |
2 | 2 |
|
3 | 3 | This will walk through the process of creating the required tables, and entity framework capabilities, etc. needed for the `Employee` within a Microsoft SQL Server database. All of this work will occur within the context of the `MyEf.Hr.Database` project.
|
4 | 4 |
|
5 | 5 | The [`Beef.Database.SqlServer`](../../../tools/Beef.Database.SqlServer) and [`DbEx`](https://github.com/Avanade/dbex) provide the capabilities that will be leveraged. The underlying [documentation](https://github.com/Avanade/dbex#readme) describes these capabilities and the database approach in greater detail.
|
6 | 6 |
|
7 |
| -_Note:_ Any time that command line execution is requested, this should be performed from the base `MyEf.Hr.Database` folder. To see all supported command line options execute `dotnet run -- --help`. |
| 7 | +<br/> |
| 8 | + |
| 9 | +## Terminal |
| 10 | + |
| 11 | +The database codegen and DbEx commands will run from a command-line interface. In preparation for the steps below, open a new terminal/command-prompt and navigate to the `MyEf.Hr.Database` base folder directory |
| 12 | + |
| 13 | +_Note: To see all supported command line options execute `dotnet run -- --help`._ |
8 | 14 |
|
9 | 15 | <br/>
|
10 | 16 |
|
@@ -45,6 +51,7 @@ Create the `Hr` schema using the database tooling. The following command will cr
|
45 | 51 | dotnet run script schema Hr
|
46 | 52 | ```
|
47 | 53 |
|
| 54 | +<br/> |
48 | 55 |
|
49 | 56 | ## Create Employee table
|
50 | 57 |
|
@@ -137,16 +144,19 @@ dotnet run script refdata Hr USState
|
137 | 144 |
|
138 | 145 | Now that the Reference Data tables exist they will need to be populated. It is recommended that where possible that the Production environment values are specified (as these are intended to be deployed to all environments).
|
139 | 146 |
|
140 |
| -These values (database rows) are specified using YAML. For brevity in this document, copy the data for the above tables **only** (for now) from [`RefData.yaml`](../MyEf.Hr.Database/Data/RefData.yaml) replacing the contents of the prefilled `RefData.yaml` within the `My.Hr.Database/Data` folder. Finally, remove the `PerformanceOutcome` lines at the end of the file. |
| 147 | +These values (database rows) are specified using YAML. For brevity in this document we will grab the data we need from [`RefData.yaml`](../MyEf.Hr.Database/Data/RefData.yaml). Take the contents of this file, copy and replace the contents of the prefilled `RefData.yaml` within the `My.Hr.Database/Data` folder. Finally, remove the `PerformanceOutcome` lines at the end of the file (we are not creating or populating this table for now). |
141 | 148 |
|
142 |
| -_Note:_ The format and hierarchy for the YAML, is: Schema, Table, Row. For reference data tables where only `Code: Text` is provided, this is treated as a special case shorthand to update those two columns accordingly (the other columns will be updated automatically). The `$` prefix for a table indicates a `merge` versus an `insert` (default). |
| 149 | +_Note: The format and hierarchy for the YAML, is: Schema, Table, Row. For reference data tables where only `Code: Text` is provided, this is treated as a special case shorthand to update those two columns accordingly (the other columns will be updated automatically). The `$` prefix for a table indicates a `merge` versus an `insert` (default)._ |
143 | 150 |
|
144 | 151 | ``` yaml
|
145 | 152 | Hr:
|
146 | 153 | - $Gender:
|
147 | 154 | - F: Female
|
148 | 155 | - M: Male
|
149 | 156 | - N: Not specified
|
| 157 | + - $TerminationReason: |
| 158 | + - RE: Resigned |
| 159 | + ... |
150 | 160 | ...
|
151 | 161 | ```
|
152 | 162 |
|
|
156 | 166 |
|
157 | 167 | To support the requirement to query the Reference Data values from the database we will use Entity Framework (EF) to simplify. The Reference Data table configuration will drive the EF .NET (C#) model code-generation via the `efModel: true` attribute.
|
158 | 168 |
|
159 |
| -Remove all existing configuration from `database.beef-5.yaml` and replace. Each table configuration is referencing the underlying table and schema, then requesting an EF model is created for all related columns found within the database. _Beef_ will query the database to infer the columns during code-generation to ensure it "understands" the latest configuration. |
| 169 | +Remove all existing configuration from `database.beef-5.yaml` and replace with the contents below. Each table configuration is referencing the underlying table and schema, then requesting an EF model is created for all related columns found within the database. _Beef_ will query the database to infer the columns during code-generation to ensure it "understands" the latest configuration. |
160 | 170 |
|
161 | 171 | ``` yaml
|
162 | 172 | # Configuring the code-generation global settings
|
@@ -234,19 +244,46 @@ dotnet run codegen
|
234 | 244 | dotnet run database
|
235 | 245 | ```
|
236 | 246 |
|
237 |
| -This should create migrations script files with names similar as follows (as well as a number of other SQL and .NET related artefacts). |
| 247 | +<br/> |
| 248 | + |
| 249 | +## Verify |
| 250 | + |
| 251 | +At this stage we now have a working database ready for the consuming API logic to be added. The required database tables exist, the Reference Data data has been loaded, the required stored procedures and user-defined type (UDT) for the Event outbox have been generated and added to the database. The .NET (C#) Entity Framework models have been generated and added to the `My.Hr.Business` project, including the requisite event outbox enqueue/dequeue capabilities. |
| 252 | + |
| 253 | +To verify, confirm you have the below set of sql migration scripts: |
238 | 254 |
|
239 | 255 | ```
|
240 | 256 | └── Migrations
|
241 |
| - └── 20210430-170605-create-01-create-outbox-schema.sql |
242 |
| - └── 20210430-170605-create-01-create-outbox-eventoutbox-table.sql |
243 |
| - └── 20210430-170605-create-01-create-outbox-eventoutboxdata-table.sql |
| 257 | + └── <date>-<number>-create-hr-schema.sql |
| 258 | + └── <date>-<number>-create-hr-employee-table.sql |
| 259 | + └── <date>-<number>-create-hr-emergencycontact-table.sql |
| 260 | + └── <date>-<number>-create-hr-gender-refdata-table.sql |
| 261 | + └── <date>-<number>-create-hr-terminationreason-refdata-table.sql |
| 262 | + └── <date>-<number>-create-hr-relationshiptype-refdata-table.sql |
| 263 | + └── <date>-<number>-create-hr-usstate-refdata-table.sql |
| 264 | + └── <date>-<number>-01-create-outbox-schema.sql |
| 265 | + └── <date>-<number>-02-create-outbox-eventoutbox-table.sql |
| 266 | + └── <date>-<number>-03-create-outbox-eventoutboxdata-table.sql |
244 | 267 | ```
|
245 | 268 |
|
246 |
| -<br/> |
| 269 | +Confirm also, the following DB and tables have been created (utilizing a DB tool such as Azure Data Explorer or SSMS): |
| 270 | +``` |
| 271 | +└── Local |
| 272 | + └── Databases |
| 273 | + └── MyEf.Hr |
| 274 | + └── Tables |
| 275 | + └── Hr.EmergencyContact |
| 276 | + └── Hr.Employee |
| 277 | + └── Hr.Gender |
| 278 | + └── Hr.RelationshipType |
| 279 | + └── Hr.TerminationReason |
| 280 | + └── Hr.USState |
| 281 | + └── Outbox.EventOutbox |
| 282 | + └── Outbox.EventOutboxData |
| 283 | +``` |
247 | 284 |
|
248 |
| -## Conclusion |
| 285 | +<br/> |
249 | 286 |
|
250 |
| -At this stage we now have a working database ready for the consuming API logic to be added. The required database tables exist, the Reference Data data has been loaded, the required stored procedures and user-defined type (UDT) for the Event outbox have been generated and added to the database. The .NET (C#) Entity Framework models have been generated and added to the `My.Hr.Business` project, including the requisite event outbox enqueue/dequeue capabilities. |
| 287 | +## Next Step |
251 | 288 |
|
252 |
| -Next we need to create the [employee API](./Employee-Api.md) endpoint to perform the desired CRUD operations. |
| 289 | +Next, we need to create the [employee API](./3-Employee-Api.md) endpoint to perform the desired CRUD operations. |
0 commit comments