ef code first
DESCRIPTION
TRANSCRIPT
EF Code First Model Key Idea
How it works?
o Database
Initialize
o Data Annotation
Dmytryi Lebedinskyi
Code First ModelCode First Model
1. Define Simple Entities with connection by ID
2. Add virtual properties for lazy loading of Aggregated Fields
EF lazy loading is enabled by default
Code First ModelCode First Model
3. Remember to preserve name convention
EF Code First uses general agreement about names.
Code First ModelCode First Model
4. Create Data Context
DbContext is similar to EF general Data Context
Code First ModelCode First Model
Code First ModelCode First Model
5. Create a Repositories
6. What about usage?
You can work with it same like a general EF Model
Code First ModelCode First Model
7. And what is the result?
Code First ModelCode First Model
Code First ModelCode First Model
9. Where is Database?
Default DB Server is .\SqlExpress
Code First ModelCode First Model
10. Change Database name
How to change DB name?
• App.config or any other Connection String supplied
•DbConnection
•Replace the Default Convention
EF Code First Model
Key Idea
How it works?
Database Initialize
o Data Annotations
Code First ModelCode First Model
System.Data.Entity.Database.IDatabaseInitializer<in TContext>
CreateDatabaseIfNotExists
DropCreateDatabaseIfModelChanges
DropCreateDatabaseAlways
Database Initializers
Code First ModelCode First Model
1. Create Initializer
Code First ModelCode First Model
2. Register Initializer
Code First ModelCode First Model
3. And we have data if no db is created
EF Code First Model
Key Idea
How it works?
Database Initialize
Data Annotations
Since EF Code First CTP5 Data Annotations can be used for DB structure
Code First ModelCode First Model
• KeyAttribute
• StringLengthAttribute
• MaxLengthAttribute
• ConcurrencyCheckAttribute
• RequiredAttribute
• TimestampAttribute
• ComplexTypeAttribute
• ColumnAttribute (Placed on a property to specify the column name, ordinal & data type)
• TableAttribute (Placed on a class to specify the table name and schema)
• InversePropertyAttribute (Placed on a navigation property to specify the property that represents the other end of a relationship)
• ForeignKeyAttribute (Placed on a navigation property to specify the property that represents the foreign key of the relationship)
• DatabaseGeneratedAttribute (Placed on a property to specify how the database generates a value for the property (Identity, Computed or None) )
• NotMappedAttribute (Placed on a property or class to exclude it from the database)
And Validation Attributes are now also supported
Code First ModelCode First Model
And using some MVC Magic we receive
Code First ModelCode First Model
Questions?
:)