Your SlideShare is downloading. ×
Entity Framework - Entity Data Model (edm)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Entity Framework - Entity Data Model (edm)

4,377

Published on

Entity Framework - Entity Data Model (edm)

Entity Framework - Entity Data Model (edm)

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,377
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Create the table in the DBAdd the mapping for Person and subclass Customer. <discriminator>PersonType</> in the Person classdiscriminator-value=“Customer” in the subclassRefactorDataProvider to load, save and delete all kind of types
  • Transcript

    • 1. Entity Data Model (EDM) Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
    • 2. EF Architecture LINQ to Entities Entity SQL EDM Conceptual Object Services Model Mapping Entity Client Provider Storage ADO.NET Provider Model SQLServer© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 3. Entity Data Model (EDM)  The EDM is the link between the model and the database.  The EDM is that it decouples your application from the underlying store. Entity Data Model (EDM) Conceptual Storage Mapping Model Model Storage (MSL) (CSDL) (SSDL)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 4. Agenda LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider  EF Architecture  Development Approaches  Entity Data Model (EDM)  Object Services  Querying & Loading Entities© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 5. LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Development Approaches© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 6. Database First  If you already have a database, the Entity Framework can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. Generated Database First Data Model DB (.edmx)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 7. Model First  If you dont yet have a database, you can begin by creating a model using the Entity Framework designer in Visual Studio.  The designer can generate DDL (data definition language) statements to create the database. Data Model Model First Generated (.edmx) DB© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 8. Code First  Whether you have an existing database or not, you can code your own classes and properties that correspond to tables and columns and use them with the Entity Framework without an .edmx file. Data Model Code First Generated (Classes) DB Generated Code First Data Model DB (Classes)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 9. Conventions  The pluralized forms of entity class names are used as table names.  Entity property names are used for column names.  Entity properties that are named ID or classnameID are recognized as primary key properties.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 10. LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Database Configuration© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 11. Database Configuration(Code First) DB Name Connection Name (no config file )© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 12. Initialization Strategy (Code First)  Initialize the database, the first time a context is used in an AppDomain.  The following initializers are supported:  CreateDatabaseIfNotExist  DropCreateDatabaseAlways  DropCreateDatabaseIfModelChanges© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 13. Seeding a Database (Code First)  The Seed method takes whatever you put in the context and saves it to the database.  you don’t need to call SaveChanges but you DO need to be sure to call the base Seed method at the end.  Database.SetInitializer( new DbInit() );© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 14. Database Configuration  Model First is different from Code First in that your Entity Data Model (EDM) already exists.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 15. LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Data Annotations© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 16. Data Annotations  Code first gives you two ways to add these configurations to your classes.  Data Annotations  Fluent API© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 17. Data Annotations  Map your code first classes to a pre-existing database.  Key  DatabaseGenerated  NotMapped  InverseProperty  ComplexType  ForeignKey  ConcurrencyCheck  TimeStamp  Table and Column MSDN link© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 18. Validation Attribures  Validation your code first classes.  Required  StringLength  MaxLength & MinLength public class ProductMD { [StringLength(50)] [Required] public object Name { get; set; } [Required] public object Weight { get; set; } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 19. Validations  DbContext automatically validates entities before saving them to the database.  Validation is performed based on model facets as well as on data annotations. Entities can also be validated on demand.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 20. Data Annotations© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 21. LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Fluent API© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 22. Fluent API  Specify additional mapping configuration that will override the default Code First conventions.  Map to an existing database schema or to affect the shape of a generated schema.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 23. Fluent API (DbModelBuilder)  Fluent API Exposed through the DbModelBuilder.  Properties with Fluent API  Types with Fluent API  Relationships with Fluent API© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 24. Fluent API (Types)  Specifying  Complex Type  Not to map a CLR entity type to a table in the database  Mapping  a CLR entity type to a specific table in the database  The table-per-hierarchy (TPH) inheritance  The table-per-type (TPT) inheritance  The table-per-concrete class (TPC) inheritance  CLR properties of an entity type to multiple tables in the database  Multiple entity types to one table in the database© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 25. Fluent API (Types) Fluent API Samples (Click)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 26. Fluent API (Relationships)  Configuring:  a one-to-zero-or-one relationship  a relationship where both ends are required  a many-to-many relationship  a relationship with one navigation property  a composite foreign key  a foreign key name that does not follow the Code First convention  Enabling cascade delete  Renaming a foreign key that is not defined in the model© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 27. Relationships (HasXXXWithXXX) Principal Dependent modelBuilder.Entity<Course>() .HasRequired( t => t.Department) .WithMany( t => t.Courses) .HasForeignKey(d => d.DepartmentID) .WillCascadeOnDelete(false);© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 28. Fluent API (Relationships) Fluent API Samples (Click)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 29. Cascade Delete  You can configure cascade delete on a relationship by using the WillCascadeOnDelete method.  You can remove these cascade delete conventions by using:  modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()  modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>() modelBuilder.Entity<Course>() .HasRequired( t => t.Department) .WithMany( t => t.Courses) .HasForeignKey(d => d.DepartmentID) .WillCascadeOnDelete(false);© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 30. Fluent API (Properties)  Primary & Foreign key  Maximum length  Required  Mapping a CLR property to a specific column in the database  Unicode content  Data type of a database column  Complex type  Optimistic concurrency token© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 31. Fluent API (Properties) Fluent API Samples (Click)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 32. LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Inheritance Mapping© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 33. Inheritance Mapping  Inheritance mapping strategies:  Table per Hierarchy (TPH)  Table per Type (TPT)  Table per Concrete Type (TPC)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 34. Table per Hierarchy (TPH)  An entire class hierarchy can be mapped to a single table.  This table includes columns for all properties of all classes in the hierarchy.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 35. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 36. TPH with Fluent API  The code will change the discriminator column name to "BillingDetailType" and the values to "BA" and "CC" for BankAccount and CreditCard respectively: Click here for more information© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 37. Table per Type (TPT)  TPT is about representing inheritance relationships as relational foreign key associations.  Every class/subclass that declares persistent properties— including abstract classes—has its own table.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 38. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 39. TPT with Fluent API  TPT mapping by using ToTable() method. Click here for more information© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 40. Table per Concrete Type (TPC)  In TPC type we use exactly one table for each (nonabstract) class.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 41. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 42. Click here for more information© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 43. Fluent API© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

    ×