IntroToEF-ShahedChowdhuri-GWU-2013-09

advertisement
Intro to Entity Framework
Don’t drown in database design during development
By Shahed Chowdhuri
WakeUpAndCode.com
@shahedC
Welcome, GWU GCC Students!
Tech Check
Are you familiar with…?
Web
Application
Development
e.g. ASP.NET
MVC
Object
Relational
Mapping
(ORM)
Entity
Framework
(ORM from
Microsoft)
You Should Know…
•
•
•
•
Object-Oriented Programming (e.g. C#)
Visual IDE (e.g. Visual Studio)
Software Frameworks (e.g. Microsoft .NET)
Relational Databases (e.g. SQL Server)
Object-Relational Mapping
DB
ORM
Web
App
Top Choices
NHibernate
• Mature, popular
Entity Framework
• Now mature, integrated
Good Comparison:
http://www.dennisdoomen.net/2013/03/entityframework-56-vs-nhibernate-3.html
Working with EF
A.
DatabaseFirst
B. ModelFirst
(EDMX)
(Visual
Designer)
C. CodeFirst
(automatic
migrations)
D. CodeFirst
(manual
migrations)
A. Database-First (EDMX)
ASP.NET
MVC
app code
DB updates
EDMX Model
DB
ADO.NET
Code
Entity Data
uses EDMX
Model
(EDMX)
A1. Generate EDMX
A2. Update Model From Database
B. Model-First (Visual Designer)
Source: MSDN
C.Code-First (automatic migrations)
ASP.NET
MVC
app code
uses
.NET
models
(handcoded)
generates
Migration
code
(autogenerated)
autoupdates
DB
D. Code-First (manual migrations)
ASP.NET
MVC
app code
uses
.NET
models
(handcoded)
generates
Migration
code
(autogenerated)
manually
updates
DB
What are we trying to solve?
Conflicts Within Organization
DB Architects
Dev
Team X
Your
Dev
Team
DB Admins
Dev
Team Y
Main Agenda
1. NuGet
4. Connection
Strings
2. Entity
Framework
5. EF
Migrations
3. Models &
Mapping
6. Process
Workflow
1. NuGet Package Manager
2. Entity Framework
3. Models & Mapping
4a. Connection Strings
For more info:
http://wakeupandcode.com/all-your-database-are-belong-to-us/
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/
5a. Enable Migrations
5b. Update Database
model
code
migration
code
+ seed
migration
code
(sql)
dev
DB
5c. Push Code + Migrate Server DB
code
push code
code
pull code
CI
Server
migrate.exe
DB
6. Process Workflow
communication
dev teams
DB
DB architects
Additional Topics
Relationships
Attributes
Relationships DBContext
.OnModelCreating()
Source: Excella Lean presentation
https://github.com/excellaco/ExcellaLean
Data-Annotation Attributes
Source: MSDN
Conclusion
Dev Team
Synchronized
Continuous
Development
Entity
Framework &
Code-First
Migrations
Database
Versioning
Server
Deployment
Downloads
http://wakeupandcode.com/downloads/
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-vsnhibernate-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-inthe-entity-framework-and-code-first.aspx
Questions?
http://WakeUpAndCode.com
@shahedC
Download