ORM & Entity Framework
Jiří Urban, Tomáš Juřička, Stanislav Čermák, Karolína Nesrstová, David Długosz, Tomáš Trčka
ORM
- Object Relational Mapping
- Programming technique for working with databases using object-oriented languages
- Allows to map database tables to classes in code, making it easier to read and manipulate data
Row VS. Object
- Always flat and two-dimensional
- Contain always only primitive data types
- Optimized for efficient data retrieval and storage
- Can be complex and hierarchical
- Can contain other objects and custom-defined types
- Optimized for code organization and logic
Entity Framework
- Popular ORM framework for .NET developers
- Enables developers to interact with databases using C# or Visual Basic, without having to write SQL queries
- Supports both code -first and database-first approaches
- Includes a range of features, including LINQ support, automatic change tracking, and database migrations
Code-first
Migration
Migration commands
Add-Migration {name of the migration}
- add migration script to code
- updates database snapshot
Remove-Migration {name of the migration}
- removes migration script from code
- updates database snapshot
- does not undo applied migration scripts from database!
Update-Database
- applies migrations to database
Undo migration?
- If we ever need to undo applied migration
- Update-Database -Migration {name of the migration we want to roll-back on}
- Remove-Migration {name of the migration we want to remove}
Image 1
1
Image 2
2
Image 3
3
Page 1 Title...
Page 1 Content...
Image 4
1
Image 1
2
Page 2 Title...
Page 2 Content...
Image 2
1
Image 4
2
Page 3 Title...
Page 3 Content...
Image 2
3
Example
Page 1
Page 2
Page 3
Code-first
Migration
Migration
Model after mapping
[
{
"routeKey": "page1",
"title": "Page 1 Title...",
"content": "Page 1 Content...",
"images": [
{
"index": 1,
"fullImageUrl": "./img/1_full.jpg",
"thumbnailImageUrl": "./img/1_thumb.jpg"
},
{
"index": 2,
"fullImageUrl": "./img/1_full.jpg",
"thumbnailImageUrl": "./img/1_thumb.jpg"
},
{
"index": 3,
"fullImageUrl": "./img/1_full.jpg",
"thumbnailImageUrl": "./img/1_thumb.jpg"
}
]
},
{
"routeKey": "page2",
"title": "Page 2 Title...",
"content": "Page 2 Content...",
"images": [
{
"index": 1,
"fullImageUrl": "./img/4_full.jpg",
"thumbnailImageUrl": "./img/4_thumb.jpg"
},
{
"index": 2,
"fullImageUrl": "./img/1_full.jpg",
"thumbnailImageUrl": "./img/1_thumb.jpg"
}
]
},
{
"routeKey": "page3",
"title": "Page 3 Title...",
"content": "Page 3 Content...",
"images": [
{
"index": 1,
"fullImageUrl": "./img/2_full.jpg",
"thumbnailImageUrl": "./img/2_thumb.jpg"
},
{
"index": 2,
"fullImageUrl": "./img/4_full.jpg",
"thumbnailImageUrl": "./img/4_thumb.jpg"
},
{
"index": 3,
"fullImageUrl": "./img/2_full.jpg",
"thumbnailImageUrl": "./img/2_thumb.jpg"
}
]
}
]
Objects representation
Rows representation
VTV - 3rdLesson - ORM & Entity Framework
By daviddlugosz
VTV - 3rdLesson - ORM & Entity Framework
- 13