@ -13,30 +13,36 @@
/* buutti.css */
/* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)}
/* @theme gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}< / style > < / head > < body > < div class = "bespoke-marp-osc" > < button data-bespoke-marp-osc = "prev" tabindex = "-1" title = "Previous slide" > Previous slide< / button > < span data-bespoke-marp-osc = "page" > < / span > < button data-bespoke-marp-osc = "next" tabindex = "-1" title = "Next slide" > Next slide< / button > < button data-bespoke-marp-osc = "fullscreen" tabindex = "-1" title = "Toggle fullscreen (f)" > Toggle fullscreen< / button > < button data-bespoke-marp-osc = "presenter" tabindex = "-1" title = "Open presenter view (p)" > Open presenter view< / button > < / div > < div id = ":$p" > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "1" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
/* @theme qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}< / style > < / head > < body > < div class = "bespoke-marp-osc" > < button data-bespoke-marp-osc = "prev" tabindex = "-1" title = "Previous slide" > Previous slide< / button > < span data-bespoke-marp-osc = "page" > < / span > < button data-bespoke-marp-osc = "next" tabindex = "-1" title = "Next slide" > Next slide< / button > < button data-bespoke-marp-osc = "fullscreen" tabindex = "-1" title = "Toggle fullscreen (f)" > Toggle fullscreen< / button > < button data-bespoke-marp-osc = "presenter" tabindex = "-1" title = "Open presenter view (p)" > Open presenter view< / button > < / div > < div id = ":$p" > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "1" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h1 id = "databases-with-entity-framework" > Databases with Entity Framework< / h1 >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "2" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "2" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "2" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "2" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h2 id = "contents" > Contents< / h2 >
< ul >
< li > < a href = "#entity-framework-ef" > Entity Framework (EF)< / a > < / li >
< li > < a href = "#code-first-approach" > Code First approach< / a > < / li >
< li > < a href = "#migrations" > Migrations< / a > < / li >
< li > < a href = "#migrations" > Migrations< / a > < / li >
< li > < a href = "#database-first-approach" > Database First approach< / a > < / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "3" data-marpit-fragments = " 3 " data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "3" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "3" data-marpit-fragments = " 5 " data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "3" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h2 id = "entity-framework-ef" > Entity Framework (EF)< / h2 >
< ul >
< li data-marpit-fragment = "1" > < a href = "https://learn.microsoft.com/en-us/ef/" > Entity Framework< / a > is an Object-Relational Mapper (ORM) made by Microsoft for the .NET framework
< li data-marpit-fragment = "1" > This lecture will assume you have a basic understanding of SQL databases
< ul >
< li data-marpit-fragment = "2" > Read < a href = "https://gitea.buutti.com/education/webdev-basics/src/branch/main/sql-databases.md" > Webdev basics: SQL Databases< / a > first!< / li >
< / ul >
< / li >
< li data-marpit-fragment = "3" > < a href = "https://learn.microsoft.com/en-us/ef/" > Entity Framework< / a > is an Object-Relational Mapper (ORM) made by Microsoft for the .NET framework
< ul >
< li data-marpit-fragment = "2" > Object-Relational Mapping: converting from database representation to objects in a programming language< / li >
< li data-marpit-fragment = " 4 "> Object-Relational Mapping: converting from database representation to objects in a programming language< / li >
< / ul >
< / li >
< li data-marpit-fragment = "3" > Allows creation of CRUD operations without writing SQL< / li >
< li data-marpit-fragment = " 5 "> Allows creation of CRUD operations without writing SQL< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "4" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "4" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "4" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "4" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "entity-framework-core-ef-core" > Entity Framework Core (EF Core)< / h3 >
< ul >
< li data-marpit-fragment = "1" > < a href = "https://learn.microsoft.com/en-us/ef/core/" > EF Core< / a > is a cross-platform version of EF< / li >
@ -46,7 +52,7 @@
< li data-marpit-fragment = "5" > This is what we'll be using< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "5" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "5" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "5" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "5" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "code-first-vs-database-first-vs-model-first" > Code First vs Database First vs Model First< / h3 >
< ul >
< li data-marpit-fragment = "1" > There are three approaches through which Entity Framework can be implemented
@ -59,7 +65,7 @@
< li data-marpit-fragment = "5" > Database First and Code First are the most used ones and will be introduced in this lecture< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "6" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "6" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "6" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "6" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "note-about-loading-data" > Note about loading data< / h3 >
< ul >
< li data-marpit-fragment = "1" > In EF Core, you can use < a href = "https://learn.microsoft.com/en-us/ef/ef6/fundamentals/relationships" > navigation properties< / a > in your model to load related entities< / li >
@ -72,10 +78,10 @@
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "7" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "7" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "7" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "7" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h2 id = "code-first-approach" > Code First approach< / h2 >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "8" data-marpit-fragments = "9" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "8" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "8" data-marpit-fragments = "9" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "8" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "code-first" > Code First< / h3 >
< ul >
< li data-marpit-fragment = "1" > In the Code First approach, Entity Framework will create databases and tables based on defined < em > < strong > entity classes< / strong > < / em > < / li >
@ -95,7 +101,7 @@
< li data-marpit-fragment = "9" > Not preferred for data intensive applications< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "9" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "9" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "9" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "9" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "required-packages" > Required Packages< / h3 >
< ul >
< li data-marpit-fragment = "1" > Install and add the following packages to your project:
@ -110,7 +116,7 @@
< p > < img src = "imgs/7-databases-with-entity-framework_11.png" alt = "" style = "width:1000px;" / > < / p >
< / div >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "10" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "10" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "10" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "10" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "code-first-dbcontext" > Code First: < code > DbContext< / code > < / h3 >
< ul >
< li data-marpit-fragment = "1" > Let's begin with the Code First Approach< / li >
@ -125,7 +131,7 @@ b) classes to keep the database up-to-date with CRUD operations
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "11" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "11" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "11" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "11" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "creating-a-context" > Creating a context< / h3 >
< ul >
< li data-marpit-fragment = "1" > Create a context that inherits from < code > DbContext< / code >
@ -145,7 +151,7 @@ b) classes to keep the database up-to-date with CRUD operations
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "12" data-marpit-fragments = "2" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "12" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "12" data-marpit-fragments = "2" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "12" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< ul >
< li data-marpit-fragment = "1" > To further configure how the database will be structured, override the < code > OnModelCreating< / code > method< / li >
< li data-marpit-fragment = "2" > In this example, one table named < code > Contact< / code > with columns < code > Id< / code > , < code > Name< / code > and < code > Email< / code > will be created:< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactsContext< / span > : < span class = "hljs-title" > DbContext< / span >
@ -162,7 +168,7 @@ b) classes to keep the database up-to-date with CRUD operations
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "13" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "13" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "13" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "13" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< ul >
< li data-marpit-fragment = "1" > In this example, the Contact table will be created with some starting values for < code > Id< / code > , < code > Name< / code > and < code > Email< / code > columns:< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactsContext< / span > : < span class = "hljs-title" > DbContext< / span >
{
@ -181,7 +187,7 @@ b) classes to keep the database up-to-date with CRUD operations
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "14" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "14" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "14" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "14" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "dbcontext-as-a-service" > DbContext as a Service< / h3 >
< ul >
< li data-marpit-fragment = "1" > In < code > Program.cs< / code > , add the context to services with < code > AddDbContext< / code > method< / li >
@ -198,7 +204,7 @@ services.AddControllers().AddNewtonsoftJson();
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "15" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "15" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "15" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "15" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h2 id = "migrations" > Migrations< / h2 >
< ul >
< li data-marpit-fragment = "1" > As the development progresses, models and database schemas change over time
@ -214,7 +220,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "5" > EFCore migrations have built-in version control; a snapshot of each version of the schema is stored< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "16" data-marpit-fragments = "6" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "16" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "16" data-marpit-fragments = "6" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "16" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "applying-migrations" > Applying migrations< / h3 >
< ul >
< li data-marpit-fragment = "1" > Open the Package Manager Console in Visual Studio
@ -235,7 +241,7 @@ services.AddControllers().AddNewtonsoftJson();
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "17" data-marpit-fragments = "3" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "17" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "17" data-marpit-fragments = "3" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "17" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< ul >
< li data-marpit-fragment = "1" > At this point, the values you have entered (< code > Contacts< / code > table in this example) should show up in the database. You can check it up e.g. in pgAdmin.< / li >
< / ul >
@ -256,7 +262,7 @@ services.AddControllers().AddNewtonsoftJson();
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "18" data-marpit-fragments = "6" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "exercise invert" data-marpit-pagination = "18" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "18" data-marpit-fragments = "6" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "exercise invert" data-marpit-pagination = "18" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "exercise-1-adding-context" > Exercise 1: Adding Context< / h3 >
< p > Continue working on the CourseAPI.< / p >
@ -269,7 +275,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "6" > Check that the < code > Course< / code > table with the starting values has appeared to the database< / li >
< / ol >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "19" data-marpit-fragments = "6" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "19" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "19" data-marpit-fragments = "6" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "19" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "using-dbcontext-in-the-api" > Using DbContext in the API< / h3 >
< ul >
< li data-marpit-fragment = "1" > Because < code > DbContext< / code > is added to services, it can be accessed from any other service, such as the repository< / li >
@ -283,7 +289,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "6" > After modifying the < code > DbSet< / code > , update the changes to the database with the < code > DbContext.SaveChanges()< / code > method< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "20" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "20" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "20" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "20" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "injecting-dbcontext" > Injecting DbContext< / h3 >
< ul >
< li data-marpit-fragment = "1" > Inject the < code > DbContext< / code > to your repositories as you would any other service:< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactRepository< / span > : < span class = "hljs-title" > IContactRepository< / span >
@ -300,7 +306,7 @@ services.AddControllers().AddNewtonsoftJson();
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "21" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "21" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "21" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "21" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "dbcontext-read-operations" > DbContext: Read Operations< / h3 >
< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactRepository< / span > : < span class = "hljs-title" > IContactRepository< / span >
{
@ -315,7 +321,7 @@ services.AddControllers().AddNewtonsoftJson();
}
< / code > < / pre >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "22" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "22" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "22" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "22" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "dbcontext-create-operations" > DbContext: Create Operations< / h3 >
< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactRepository< / span > : < span class = "hljs-title" > IContactRepository< / span >
{
@ -333,7 +339,7 @@ services.AddControllers().AddNewtonsoftJson();
}
< / code > < / pre >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "23" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "23" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "23" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "23" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "dbcontext-update-operations" > DbContext: Update Operations< / h3 >
< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactRepository< / span > : < span class = "hljs-title" > IContactRepository< / span >
{
@ -354,7 +360,7 @@ services.AddControllers().AddNewtonsoftJson();
}
< / code > < / pre >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "24" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "24" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "24" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "24" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "dbcontext-delete-operations" > DbContext: Delete Operations< / h3 >
< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > ContactRepository< / span > : < span class = "hljs-title" > IContactRepository< / span >
{
@ -372,7 +378,7 @@ services.AddControllers().AddNewtonsoftJson();
}
< / code > < / pre >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "25" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "exercise invert" data-marpit-pagination = "25" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "25" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "exercise invert" data-marpit-pagination = "25" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "exercise-2-crud-on-the-db" > Exercise 2: CRUD on the DB< / h3 >
< p > Continue working on CourseAPI.< / p >
@ -381,7 +387,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "2" > Test with Postman. Keep refreshing the DB in pgAdmin or creating queries with psql to make sure the requests work as intended< / li >
< / ol >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "26" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "26" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "26" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "26" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "summing-things-up" > Summing Things Up< / h3 >
< ul >
< li data-marpit-fragment = "1" > Now the API has been hooked up to a PostgreSQL database< / li >
@ -390,7 +396,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "4" > Controllers accepting HTTP requests have access to the repository< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "27" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "27" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "27" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "27" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "efcore-code-first-checklist" > EFCore Code First Checklist< / h3 >
< ol >
< li data-marpit-fragment = "1" > Install required packages< / li >
@ -400,7 +406,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "5" > Add CRUD operations to the database repository< / li >
< / ol >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "28" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "28" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "28" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "28" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "modifying-the-relations" > Modifying the Relations< / h3 >
< ul >
< li data-marpit-fragment = "1" > Let's change the structure of our Contacts API by adding a new class < code > Account< / code >
@ -412,7 +418,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "4" > Emails will be removed from the < code > Contacts< / code > table< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "29" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "29" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "29" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "29" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< pre is = "marp-pre" data-auto-scaling = "downscale-only" > < code class = "language-csharp" > < span class = "hljs-comment" > // Models/Contact.cs< / span >
< span class = "hljs-keyword" > public< / span > < span class = "hljs-keyword" > class< / span > < span class = "hljs-title" > Contact< / span >
{
@ -432,13 +438,13 @@ services.AddControllers().AddNewtonsoftJson();
}
< / code > < / pre >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "30" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "30" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "30" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "30" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< ul >
< li data-marpit-fragment = "1" > Adding a migration at this point will result in a warning:< br / >
< img src = "imgs/7-databases-with-entity-framework_14.png" alt = "" style = "width:1000px;" / > < / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "31" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "31" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "31" data-marpit-fragments = "5" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "31" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< div class = 'columns32' markdown = '1' >
< div markdown = '1' >
< ul >
@ -460,7 +466,7 @@ services.AddControllers().AddNewtonsoftJson();
< / div >
< / div >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "32" data-marpit-fragments = "4" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "exercise invert" data-marpit-pagination = "32" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "32" data-marpit-fragments = "4" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "exercise invert" data-marpit-pagination = "32" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "exercise-3-adding-migrations" > Exercise 3: Adding Migrations< / h3 >
< p > Continue working on CourseAPI.< / p >
@ -471,10 +477,10 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "4" > Update the database. Check that the changes show up in the database with pgAdmin< / li >
< / ol >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "33" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "33" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "33" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "33" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h2 id = "database-first-approach" > Database First approach< / h2 >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "34" data-marpit-fragments = "7" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "34" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "34" data-marpit-fragments = "7" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "34" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "what-is-the-database-first-approach" > What is the Database First approach?< / h3 >
< ul >
< li data-marpit-fragment = "1" > This is the other approach for creating a connection between the database and the application< / li >
@ -489,7 +495,7 @@ services.AddControllers().AddNewtonsoftJson();
< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "35" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "35" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "35" data-marpit-fragments = "4" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "35" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "scaffolding" > Scaffolding< / h3 >
< ul >
< li data-marpit-fragment = "1" > Use the Package Manager Console to " reverse engineer" the code for an existing database
@ -503,7 +509,7 @@ services.AddControllers().AddNewtonsoftJson();
< li data-marpit-fragment = "4" > Using the connection string corresponding to your database, this will create all the classes for the entities in the DB as well as the context class< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "36" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "exercise invert" data-marpit-pagination = "36" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "36" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "exercise invert" data-marpit-pagination = "36" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "exercise-4-database-first" > Exercise 4: Database First< / h3 >
< p > Create a new ASP.NET Core web app using the API template.< / p >
@ -514,13 +520,13 @@ b) by copying the <code><PackageReference></code> lines from the <code>.cs
< li data-marpit-fragment = "2" > Scaffold the < code > sqlpractice< / code > database created in < a href = "sql-databases#exercise-1-preparing-the-database" > SQL Databases Exercise 1< / a > to the project by using the Database First approach. If you have not yet created the database in PostgreSQL, it can be found < a href = "code-examples/example-query.sql" > here< / a > < / li >
< / ol >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "37" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "invert" data-marpit-pagination = "37" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "37" data-marpit-fragments = "1" data-paginate = "true" data-class = "invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "invert" data-marpit-pagination = "37" style = "--paginate:true;--class:invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "reading-authentication-with-roles" > Reading: Authentication with roles< / h3 >
< ul >
< li data-marpit-fragment = "1" > < a href = "https://www.c-sharpcorner.com/article/jwt-token-creation-authentication-and-authorization-in-asp-net-core-6-0-with-po/" > Here's< / a > an example how to do a role-based authentication by using JWT tokens< / li >
< / ul >
< / section >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "38" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh " lang = "en-US" class = "exercise invert" data-marpit-pagination = "38" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: gqy7k3kdx5d6t13nmmhm7jdgeg2yxye7r5enweiq2sgh ;" data-marpit-pagination-total = "38" >
< / foreignObject > < / svg > < svg data-marpit-svg = "" viewBox = "0 0 1280 720" > < foreignObject width = "1280" height = "720" > < section id = "38" data-marpit-fragments = "2" data-paginate = "true" data-class = "exercise invert" data-heading-divider = "5" data-theme = " qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 " lang = "en-US" class = "exercise invert" data-marpit-pagination = "38" style = "--paginate:true;--class:exercise invert;--heading-divider:5;--theme: qxwzalhzytjx5wnlf6losqvbl44tf0bt7if1oe7ch9 ;" data-marpit-pagination-total = "38" >
< h3 id = "exercise-5-extra-connection" > Exercise 5 (Extra): Connection< / h3 >
< p > Continuing the previous exercise,< / p >