Boutique product development company
It is amazing what you can accomplish when you have a client-centric team to deliver ...
Entity Framework Code-First
Syed Awn Ali

Syed Awn Ali
Entity Framework
• Object/Relational Mapping (O/RM)
• Enhancement to ADO.NET
• Why Entity Framework?
– Domain objects to r...
Types Of EF
• Code First
– Write your classes first

• Model First
– Create Entities, relationships, and inheritance
hiera...
Code First EF
• Starts from Entity Framework 4.1
• Features
– Useful in Domain Driven Design
– One-to-one, one-to-many and...
Basic Workflow
•
•
•
•

Create classes
Create new database
Map your classes
Basic workflow:
– Write code-first application...
Simple Code First Example
• Quick Start Example Demonstration

Syed Awn Ali
Database Initialization

Syed Awn Ali
Database Initialization
• No Parameter:
– Creates the database in your local SQLEXPRESS
with name matches your {Namespace}...
Database Initialization
• Name:
– Creates database in the local SQLEXPRESS db
server using that name

Syed Awn Ali
Database Initialization
• ConnectionStringName:
– Create the database as per connection string

Syed Awn Ali
Database Initialization Strategies
• CreateDatabaseIfNotExists:
– Default initializer
– Create the database if not exists
...
Turn off DB Initializer
• Production environment you don’t want to
lose existing data

Syed Awn Ali
Seed Database
• Provide some test data for your application

Syed Awn Ali
Configure Domain Classes
• There are two ways by which you can
configure your domain classes:
– DataAnnotation
– Fluent AP...
DataAnnotation
• Attribute based configuration
• System.ComponentModel.DataAnnotations
namespace
• Provides only subset of...
DataAnnotation
• Example Code:

Syed Awn Ali
DataAnnotation
• Validation Attributes:

Syed Awn Ali
DataAnnotation
• Database Schema related Attributes:

Syed Awn Ali
DataAnnotation

Syed Awn Ali
Fluent API
• Don’t find some attributes in DataAnnotation
then you have to use Fluent API
• Example:

Syed Awn Ali
Fluent API
• EntityTypeConfiguration Class:
• Class that allows configuration to be
performed for an entity type in a mode...
EntityTypeConfiguration Class
• EntityTypeConfiguration has following
important methods

Syed Awn Ali
Fluent API classes
• Level 1:
– DbModelBuilder Class
• Main class

• Level 2:
– EntityTypeConfiguration
• Set relationship...
One-to-One Relationship
• When primary key of one table becomes PK & FK
in another table

• Quick Start Example:
Syed Awn ...
One-to-Many Relationship

Syed Awn Ali
Many-to-Many Relationship

Syed Awn Ali
Migration in Code-First
• Database initialization strategies:
– CreateDatabaseIfNotExists
– DropCreateDatabaseIfModelChang...
Automated Migration
 Don’t have to maintain database migration manually in code

file
 Process:

 Comman: enable-migrat...
Automated Migration
• Oops … If your Database tables already have data?

• AutomaticMigrationDataLossAllowed = true
• get-...
Code-based Migration
• Commands:
– Add-migration
• Scaffold the next migration for the changes you have made

– Update-dat...
References
• http://www.entityframeworktutorial.net
• http://weblogs.asp.net/scottgu/archive/2010/
07/16/code-first-develo...
Thank you 

Syed Awn Ali
Upcoming SlideShare
Loading in …5
×

Entity framework code first

2,939 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,939
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
57
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Entity framework code first

  1. 1. Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products. Syed Awn Ali
  2. 2. Entity Framework Code-First Syed Awn Ali Syed Awn Ali
  3. 3. Entity Framework • Object/Relational Mapping (O/RM) • Enhancement to ADO.NET • Why Entity Framework? – Domain objects to relational database without much programming – Maintainable and extendable – Automates CRUD • Other ORMs – DataObjects.Net, NHibernate, OpenAccess, SubSonic Syed Awn Ali
  4. 4. Types Of EF • Code First – Write your classes first • Model First – Create Entities, relationships, and inheritance hierarchies • Database First – Generate EDMX from existing database Syed Awn Ali
  5. 5. Code First EF • Starts from Entity Framework 4.1 • Features – Useful in Domain Driven Design – One-to-one, one-to-many and many-to-many relationship – DataAnnotation – Fluent API • Prerequisites: – .Net Framework3.5, C#, Visual Studio 2010 and MS SQL Server Syed Awn Ali
  6. 6. Basic Workflow • • • • Create classes Create new database Map your classes Basic workflow: – Write code-first application classes – Hit F5 to run the application – Code First API creates new database or map with existing database from application classes – Inserts default/test data into the database – Finally launch the application Syed Awn Ali
  7. 7. Simple Code First Example • Quick Start Example Demonstration Syed Awn Ali
  8. 8. Database Initialization Syed Awn Ali
  9. 9. Database Initialization • No Parameter: – Creates the database in your local SQLEXPRESS with name matches your {Namespace}.{Context class name} Syed Awn Ali
  10. 10. Database Initialization • Name: – Creates database in the local SQLEXPRESS db server using that name Syed Awn Ali
  11. 11. Database Initialization • ConnectionStringName: – Create the database as per connection string Syed Awn Ali
  12. 12. Database Initialization Strategies • CreateDatabaseIfNotExists: – Default initializer – Create the database if not exists • DropCreateDatabaseIfModelChanges: – Creates new database if your model classes have been changed • DropCreateDatabaseAlways: – Drops and Creates an existing database every time • Custom DB Initializer – Create your own custom initializer Syed Awn Ali
  13. 13. Turn off DB Initializer • Production environment you don’t want to lose existing data Syed Awn Ali
  14. 14. Seed Database • Provide some test data for your application Syed Awn Ali
  15. 15. Configure Domain Classes • There are two ways by which you can configure your domain classes: – DataAnnotation – Fluent API Syed Awn Ali
  16. 16. DataAnnotation • Attribute based configuration • System.ComponentModel.DataAnnotations namespace • Provides only subset of Fluent API • Don’t find some attributes in DataAnnotation then you have to use Fluent API Syed Awn Ali
  17. 17. DataAnnotation • Example Code: Syed Awn Ali
  18. 18. DataAnnotation • Validation Attributes: Syed Awn Ali
  19. 19. DataAnnotation • Database Schema related Attributes: Syed Awn Ali
  20. 20. DataAnnotation Syed Awn Ali
  21. 21. Fluent API • Don’t find some attributes in DataAnnotation then you have to use Fluent API • Example: Syed Awn Ali
  22. 22. Fluent API • EntityTypeConfiguration Class: • Class that allows configuration to be performed for an entity type in a model • Obtained by calling Entity method of DbModelBuilder class Syed Awn Ali
  23. 23. EntityTypeConfiguration Class • EntityTypeConfiguration has following important methods Syed Awn Ali
  24. 24. Fluent API classes • Level 1: – DbModelBuilder Class • Main class • Level 2: – EntityTypeConfiguration • Set relationship between entities • Level 3: – ManyNavigarionConfiguration – OptionalNavigationPropertyConfiguration – RequiredNavigationPropertyConfiguration • Level 1 and Level 2 classes can be used to configure relationship between the entities that will be mapped to database tables • Level 3 & 4 can be used to configure additional mapping between the entities. Syed Awn Ali
  25. 25. One-to-One Relationship • When primary key of one table becomes PK & FK in another table • Quick Start Example: Syed Awn Ali
  26. 26. One-to-Many Relationship Syed Awn Ali
  27. 27. Many-to-Many Relationship Syed Awn Ali
  28. 28. Migration in Code-First • Database initialization strategies: – CreateDatabaseIfNotExists – DropCreateDatabaseIfModelChanges – DropCreateDatabaseAlways • Problem: – Already have data – Existing Stored Procedures – Triggers • Kinds of Migration: – Automated Migration – Code based Migration Syed Awn Ali
  29. 29. Automated Migration  Don’t have to maintain database migration manually in code file  Process:  Comman: enable-migrations –EnableAutomaticMigration:$true  Need to set the database initializer  Run the application and see the created database  System table __MigrationHistory Syed Awn Ali
  30. 30. Automated Migration • Oops … If your Database tables already have data? • AutomaticMigrationDataLossAllowed = true • get-help enable-migrations Syed Awn Ali
  31. 31. Code-based Migration • Commands: – Add-migration • Scaffold the next migration for the changes you have made – Update-database • Apply pending changes to the database • –verbose to see what’s going on in the database • Rollback Database change – Update-database -TargetMigration:"First School DB schema“ • Get All Migrations – Use "get-migration" command to see what migration have been applied. Syed Awn Ali
  32. 32. References • http://www.entityframeworktutorial.net • http://weblogs.asp.net/scottgu/archive/2010/ 07/16/code-first-development-with-entityframework-4.aspx • http://msdn.microsoft.com/enus/data/jj591620.aspx • http://blogs.msdn.com/b/efdesign/archive/20 10/06/01/conventions-for-code-first.aspx Syed Awn Ali
  33. 33. Thank you  Syed Awn Ali

×