|
| 1 | +--- |
| 2 | +title: Build a C# application that uses Entity Framework and YSQL |
| 3 | +headerTitle: Build a C# application |
| 4 | +linkTitle: C# |
| 5 | +description: Build a C# application that uses Entity Framework and the YSQL API. |
| 6 | +menu: |
| 7 | + latest: |
| 8 | + identifier: build-apps-csharp-3-ysql |
| 9 | + parent: build-apps |
| 10 | + weight: 556 |
| 11 | +isTocNested: true |
| 12 | +showAsideToc: true |
| 13 | +--- |
| 14 | + |
| 15 | +<ul class="nav nav-tabs-alt nav-tabs-yb"> |
| 16 | + |
| 17 | + <li > |
| 18 | + <a href="../ysql/" class="nav-link"> |
| 19 | + <i class="icon-postgres" aria-hidden="true"></i> |
| 20 | + YSQL |
| 21 | + </a> |
| 22 | + </li> |
| 23 | + <li> |
| 24 | + <a href="../ysql-entity-framework/" class="nav-link active"> |
| 25 | + <i class="icon-postgres" aria-hidden="true"></i> |
| 26 | + YSQL - Entity Framework |
| 27 | + </a> |
| 28 | + </li> |
| 29 | + <li> |
| 30 | + <a href="../ycql/" class="nav-link"> |
| 31 | + <i class="icon-cassandra" aria-hidden="true"></i> |
| 32 | + YCQL |
| 33 | + </a> |
| 34 | + </li> |
| 35 | +</ul> |
| 36 | + |
| 37 | +The following tutorial implements a REST API server using the [Entity Framework](https://docs.microsoft.com/en-us/ef/) ORM. The scenario is that of an e-commerce application where database access is managed using the [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/). It includes the following tables: |
| 38 | + |
| 39 | +- `users` — the users of the e-commerce site |
| 40 | +- `products` — the products being sold |
| 41 | +- `orders` — the orders placed by the users |
| 42 | +- `orderline` — each line item of an order |
| 43 | + |
| 44 | +The source for the above application can be found in the [Using ORMs with YugabyteDB](https://github.com/yugabyte/orm-examples/tree/master/csharp/entityframework) repository. |
| 45 | + |
| 46 | +## Prerequisites |
| 47 | + |
| 48 | +The tutorial assumes that you have: |
| 49 | + |
| 50 | +- YugabyteDB up and running. If you are new to YugabyteDB, follow the steps in [Quick start](../../../../quick-start/) to have YugabyteDB up and running in minutes. |
| 51 | + |
| 52 | +- [.NET framework](https://dotnet.microsoft.com/en-us/download) installed. |
| 53 | + |
| 54 | +## Clone the "orm-examples" repository |
| 55 | + |
| 56 | +```sh |
| 57 | +$ git clone https://github.com/yugabyte/orm-examples.git && cd orm-examples/csharp/entityframework |
| 58 | +``` |
| 59 | + |
| 60 | +## Database configuration |
| 61 | + |
| 62 | +- To modify the database connection settings, change the default `ConnectionStrings` in `appsettings.json` file which is in the following format: |
| 63 | + |
| 64 | +`Host=$hostName; Port=$dbPort; Username=$dbUser; Password=$dbPassword; Database=$database` |
| 65 | + |
| 66 | +| Properties | Description | Default | |
| 67 | +| :--------- | :---------- | :------ | |
| 68 | +| Host | Database server IP address or DNS name. | 127.0.0.1 | |
| 69 | +| Port | Database port where it accepts client connections. | 5433 | |
| 70 | +| Username | The username to connect to the database. | yugabyte | |
| 71 | +| Password | The password to connect to the database. | yugabyte | |
| 72 | +| Database | Database instance in database server. | ysql_entityframework | |
| 73 | + |
| 74 | +## Start the REST API server |
| 75 | + |
| 76 | +To change default port for REST API Server, go to `Properties/launchSettings.json` and change the `applicationUrl` field. |
| 77 | + |
| 78 | +- Build the REST API server. |
| 79 | + |
| 80 | +```sh |
| 81 | +$ dotnet build |
| 82 | +``` |
| 83 | + |
| 84 | +- Run the REST API server |
| 85 | + |
| 86 | +```sh |
| 87 | +$ dotnet run |
| 88 | +``` |
| 89 | + |
| 90 | +The REST server will run at `http://localhost:8080` by default. |
| 91 | + |
| 92 | +## Send requests to the application |
| 93 | + |
| 94 | +Create 2 users. |
| 95 | + |
| 96 | +```sh |
| 97 | +$ curl --data '{ "firstName" : "John", "lastName" : "Smith", "email" : "[email protected]" }' \ |
| 98 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/users |
| 99 | +``` |
| 100 | + |
| 101 | +```sh |
| 102 | +$ curl --data '{ "firstName" : "Tom", "lastName" : "Stewart", "email" : "[email protected]" }' \ |
| 103 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/users |
| 104 | +``` |
| 105 | + |
| 106 | +Create 2 products. |
| 107 | + |
| 108 | +```sh |
| 109 | +$ curl \ |
| 110 | + --data '{ "productName": "Notebook", "description": "200 page notebook", "price": 7.50 }' \ |
| 111 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/products |
| 112 | +``` |
| 113 | + |
| 114 | +```sh |
| 115 | +$ curl \ |
| 116 | + --data '{ "productName": "Pencil", "description": "Mechanical pencil", "price": 2.50 }' \ |
| 117 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/products |
| 118 | +``` |
| 119 | + |
| 120 | +Verify the `userId` and `productId` from the database using the following YSQL commands. |
| 121 | + |
| 122 | +```sh |
| 123 | +yugabyte=# \c ysql_entityframework |
| 124 | +``` |
| 125 | + |
| 126 | +```output |
| 127 | +You are now connected to database "ysql_entityframework" as user "yugabyte". |
| 128 | +``` |
| 129 | + |
| 130 | +```sh |
| 131 | +ysql_entityframework=# SELECT count(*) FROM users; |
| 132 | +``` |
| 133 | + |
| 134 | +```output |
| 135 | + user_id | first_name | last_name | user_email |
| 136 | +---------+------------+-----------+---------------------- |
| 137 | + 1 | John | Smith | [email protected] |
| 138 | + 101 | Tom | Stewart | [email protected] |
| 139 | +(2 rows) |
| 140 | +``` |
| 141 | + |
| 142 | +```sh |
| 143 | +ysql_entityframework=# SELECT count(*) FROM products; |
| 144 | +``` |
| 145 | + |
| 146 | +```output |
| 147 | + product_id | description | price | product_name |
| 148 | +------------+-------------------+-------+-------------- |
| 149 | + 1 | 200 page notebook | 7.50 | Notebook |
| 150 | + 101 | Mechanical pencil | 2.50 | Pencil |
| 151 | +(2 rows) |
| 152 | +``` |
| 153 | + |
| 154 | +Create 2 orders with products using the `userId` for John. |
| 155 | + |
| 156 | +```sh |
| 157 | +$ curl \ |
| 158 | + --data '{ "userId": "1", "products": [ { "productId": 1, "units": 2 } ] }' \ |
| 159 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders |
| 160 | +``` |
| 161 | + |
| 162 | +```sh |
| 163 | +$ curl \ |
| 164 | + --data '{ "userId": "1", "products": [ { "productId": 1, "units": 2 }, { "productId": 101, "units": 4 } ] }' \ |
| 165 | + -v -X POST -H 'Content-Type:application/json' http://localhost:8080/orders |
| 166 | +``` |
| 167 | + |
| 168 | +## Query results |
| 169 | + |
| 170 | +### Using the YSQL shell |
| 171 | + |
| 172 | +```sql |
| 173 | +ysql_entityframework=# SELECT count(*) FROM users; |
| 174 | +``` |
| 175 | + |
| 176 | +```output |
| 177 | + count |
| 178 | +------- |
| 179 | + 2 |
| 180 | +(1 row) |
| 181 | +``` |
| 182 | + |
| 183 | +```sql |
| 184 | +ysql_entityframework=# SELECT count(*) FROM products; |
| 185 | +``` |
| 186 | + |
| 187 | +```output |
| 188 | + count |
| 189 | +------- |
| 190 | + 2 |
| 191 | +(1 row) |
| 192 | +``` |
| 193 | + |
| 194 | +```sql |
| 195 | +ysql_entityframework=# SELECT count(*) FROM orders; |
| 196 | +``` |
| 197 | + |
| 198 | +```output |
| 199 | + count |
| 200 | +------- |
| 201 | + 2 |
| 202 | +(1 row) |
| 203 | +``` |
| 204 | + |
| 205 | +### Using the REST API |
| 206 | + |
| 207 | +Verify the users, products, and orders created in the `ysql_entityframework` database from the REST API server. |
| 208 | + |
| 209 | +```sh |
| 210 | +$ curl http://localhost:8080/users |
| 211 | +``` |
| 212 | + |
| 213 | +```output.json |
| 214 | +{ |
| 215 | + "content": [ |
| 216 | + { |
| 217 | + "userId": 101, |
| 218 | + "firstName": "Tom", |
| 219 | + "lastName": "Stewart", |
| 220 | + |
| 221 | + }, |
| 222 | + { |
| 223 | + "userId": 1, |
| 224 | + "firstName": "John", |
| 225 | + "lastName": "Smith", |
| 226 | + |
| 227 | + } |
| 228 | + ], |
| 229 | + ... |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +```sh |
| 234 | +$ curl http://localhost:8080/products |
| 235 | +``` |
| 236 | + |
| 237 | +```output.json |
| 238 | +{ |
| 239 | + "content": [ |
| 240 | + { |
| 241 | + "productId": 101, |
| 242 | + "productName": "Pencil", |
| 243 | + "description": "Mechanical pencil", |
| 244 | + "price": 2.5 |
| 245 | + }, |
| 246 | + { |
| 247 | + "productId": 1, |
| 248 | + "productName": "Notebook", |
| 249 | + "description": "200 page notebook", |
| 250 | + "price": 7.5 |
| 251 | + } |
| 252 | + ], |
| 253 | + ... |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +```sh |
| 258 | +$ curl http://localhost:8080/orders |
| 259 | +``` |
| 260 | + |
| 261 | +```output.json |
| 262 | +{ |
| 263 | + "content": [ |
| 264 | + { |
| 265 | + "orderId":"2692e1e9-0bbd-40e8-bf51-4fbcc4e9fea2", |
| 266 | + "orderTime":"2022-02-24T02:32:52.60555", |
| 267 | + "orderTotal":15.00, |
| 268 | + "userId":1, |
| 269 | + "users":null, |
| 270 | + "products":null |
| 271 | + }, |
| 272 | + { |
| 273 | + "orderId":"f7343f22-7dfc-4a18-b4d3-9fcd17161518", |
| 274 | + "orderTime":"2022-02-24T02:33:06.832663", |
| 275 | + "orderTotal":25.00, |
| 276 | + "userId":1, |
| 277 | + "users":null, |
| 278 | + "products":null |
| 279 | + } |
| 280 | + ] |
| 281 | +} |
| 282 | +``` |
| 283 | + |
| 284 | +## Explore the source |
| 285 | + |
| 286 | +The application source is available in the [orm-examples](https://github.com/yugabyte/orm-examples/tree/master/csharp/entityframework) repository. |
0 commit comments