Ef code first

1,998 views
1,912 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,998
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ef code first

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

×