ef code first

20
EF Code First Model Key Idea How it works? o Database Initialize o Data Annotation Dmytryi Lebedinskyi

Upload: dotnetusergroupdnipro

Post on 23-Jan-2015

1.788 views

Category:

Documents


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Ef code first

EF Code First Model Key Idea

How it works?

o Database

Initialize

o Data Annotation

Dmytryi Lebedinskyi

Page 2: Ef code first

Code First ModelCode First Model

1. Define Simple Entities with connection by ID

Page 3: Ef code first

2. Add virtual properties for lazy loading of Aggregated Fields

EF lazy loading is enabled by default

Code First ModelCode First Model

Page 4: Ef code first

3. Remember to preserve name convention

EF Code First uses general agreement about names.

Code First ModelCode First Model

Page 5: Ef code first

4. Create Data Context

DbContext is similar to EF general Data Context

Code First ModelCode First Model

Page 6: Ef code first

Code First ModelCode First Model

5. Create a Repositories

Page 7: Ef code first

6. What about usage?

You can work with it same like a general EF Model

Code First ModelCode First Model

Page 8: Ef code first

7. And what is the result?

Code First ModelCode First Model

Page 9: Ef code first

Code First ModelCode First Model

9. Where is Database?

Default DB Server is .\SqlExpress

Page 10: Ef code first

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 

Page 11: Ef code first

EF Code First Model

Key Idea

How it works?

Database Initialize

o Data Annotations

Page 12: Ef code first

Code First ModelCode First Model

System.Data.Entity.Database.IDatabaseInitializer<in TContext>

CreateDatabaseIfNotExists

DropCreateDatabaseIfModelChanges

DropCreateDatabaseAlways

Database Initializers

Page 13: Ef code first

Code First ModelCode First Model

1. Create Initializer

Page 14: Ef code first

Code First ModelCode First Model

2. Register Initializer

Page 15: Ef code first

Code First ModelCode First Model

3. And we have data if no db is created

Page 16: Ef code first

EF Code First Model

Key Idea

How it works?

Database Initialize

Data Annotations

Page 17: Ef code first

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)

Page 18: Ef code first

And Validation Attributes are now also supported

Code First ModelCode First Model

Page 19: Ef code first

And using some MVC Magic we receive

Code First ModelCode First Model

Page 20: Ef code first

Questions?

:)