Download - Intro to Entity Framework
![Page 1: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/1.jpg)
Intro to Entity Framework
By Shahed Chowdhuri
Don’t drown in database design during development
@shahedC
WakeUpAndCode.com
![Page 2: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/2.jpg)
Welcome, GWU GCC Students!
![Page 3: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/3.jpg)
Tech Check
Web Application
Developmente.g. ASP.NET
MVC
EntityFramework(ORM from Microsoft)
Object Relational Mapping
(ORM)
Are you familiar with…?
![Page 4: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/4.jpg)
• Object-Oriented Programming (e.g. C#)• Visual IDE (e.g. Visual Studio)• Software Frameworks (e.g. Microsoft .NET)• Relational Databases (e.g. SQL Server)
You Should Know…
![Page 5: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/5.jpg)
Object-Relational Mapping
Web App
ORM
DB
![Page 6: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/6.jpg)
Top ChoicesNHibernate
• Mature, popular
Entity Framework
• Now mature, integrated
Good Comparison: http://www.dennisdoomen.net/2013/03/entity-framework-56-vs-nhibernate-3.html
![Page 7: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/7.jpg)
Working with EF
A. Database-
First(EDMX)
B. Model-First
(Visual Designer)
C. Code-First (automatic migrations)
D. Code-First
(manual migrations)
![Page 8: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/8.jpg)
A. Database-First (EDMX)
ASP.NETMVC
app code DB
ADO.NET Entity Data
Model (EDMX)
DB updates EDMX Model
Code uses EDMX
![Page 9: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/9.jpg)
A1. Generate EDMX
![Page 10: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/10.jpg)
A2. Update Model From Database
![Page 11: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/11.jpg)
B. Model-First (Visual Designer)
Source: MSDN
![Page 12: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/12.jpg)
C.Code-First (automatic migrations)ASP.NET
MVCapp code
DB
Migration code
(auto-generated)
auto-updates
uses.NET
models(hand-coded)
generates
![Page 13: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/13.jpg)
D. Code-First (manual migrations)ASP.NET
MVCapp code
DB
Migration code
(auto-generated)
manuallyupdates
uses.NET
models(hand-coded)
generates
![Page 14: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/14.jpg)
What are we trying to solve?
![Page 15: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/15.jpg)
Conflicts Within Organization
Dev Team X
Your Dev
Team
DB Admins
DB Architects
Dev Team Y
![Page 16: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/16.jpg)
Main Agenda
1. NuGet
2. Entity Framework
3. Models & Mapping
4. Connection Strings
5. EF Migrations
6. Process Workflow
![Page 17: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/17.jpg)
1. NuGet Package Manager
![Page 18: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/18.jpg)
2. Entity Framework
![Page 19: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/19.jpg)
3. Models & Mapping
![Page 20: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/20.jpg)
4a. Connection Strings
For more info:http://wakeupandcode.com/all-your-database-are-belong-to-us/
![Page 21: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/21.jpg)
4b. Connection Strings (explained)
1. How can I point to my own DB?– Each dev has own ConnectionStrings.config
2. How do I avoid checking in to Source Control?– Remove file from .csproj, keep it local
3. How will server deployments work?– Use XML Transforms, i.e. Web.Prod.Config
4. Do I have to include credentials in .config?– No, use machine access to SQL server instead
For more info:http://wakeupandcode.com/all-your-database-are-belong-to-us/
![Page 22: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/22.jpg)
5a. Enable Migrations
![Page 23: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/23.jpg)
migration code
5b. Update Database
model code
migration code + seed
devDB
(sql)
![Page 24: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/24.jpg)
5c. Push Code + Migrate Server DBcode push code
CI
code pull code
migrate.exeServerDB
![Page 25: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/25.jpg)
6. Process Workflow
dev teams DB architects
communication
DB
![Page 26: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/26.jpg)
Additional Topics
Relationships Attributes
![Page 27: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/27.jpg)
RelationshipsDBContext.OnModelCreating()
Source: Excella Lean presentationhttps://github.com/excellaco/ExcellaLean
![Page 28: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/28.jpg)
Data-Annotation Attributes
Source: MSDN
![Page 29: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/29.jpg)
Conclusion
Entity Framework &
Code-First Migrations
Dev Team Synchronize
d
Server Deployment
Database Versioning
Continuous
Development
![Page 31: Intro to Entity Framework](https://reader035.vdocuments.site/reader035/viewer/2022081520/56815e8c550346895dcd1c76/html5/thumbnails/31.jpg)
Online Resources• Entity Framework on MSDN
– http://msdn.microsoft.com/en-us/data/ef.aspx• EF Code First Migrations on MSDN:
– http://msdn.microsoft.com/en-US/data/jj591621• Web.config & configSource:
– http://wakeupandcode.com/all-your-database-are-belong-to-us/• Nhibernate vs Entity Framwork:
– http://www.dennisdoomen.net/2013/03/entity-framework-56-vs-nhibernate-3.html
• My blog post on EF Code First Migrations:– http://wakeupandcode.com/entity-framework-code-first-migrations/
• Code First Data Annotations (Julie Lerman)– http://msdn.microsoft.com/en-us/data/jj591583.aspx
• Data Annotations in the Entity Framework (MSDN) – http://
blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx