C# ADO.NETÂ
Entity Framework Basics
Intro
- DB as abstraction of real world information using
- tables, indexes, views, ....
- Developer introduces a second abstraction
- to map between DB data and App data
- Entity Framework as ORM
- ADO.NET extension
- Create model of DB - some code is generated for the App
Entity
- Data associated to some object from app point of view
- Customer object
- name, address, tel, company
- Entity as the data necessary for the app
- Three views on data:
- Physical view
- Logical view
- Conceptual view
Physical View
- Tables, keys, indexes, views, etc. that hold the data
- Optimized: DBMS handles operations efficiently
- Single customer data could be split up to multiple tables
- Maybe require multiple keys to create cohesive view
- Physical storage is efficient but hard to understand for developer
Logical View
- Combined elements required to define the data for a single object
- From DB perspective the logical view combines data found in tables using keys and other DB elements
- ADO.NET reduces amout of work by providing built-in objects, but developer has to understand the underlying physical structure
Conceptual View
- Real world view on the data
- As it applies to the object
- Presents information in understandable manner
- E.g. each customer is an entity
- The customers as a group are also entities
- Entity framework relies on model to visualize data
Entity Framework
- Stores models in XML format
- Works with any data source
- flat file
- hierarchical DBs
- relational DBs
- etc.
- Reduces work overhead
- Developer focuses on the entity not on the physical or logical structure
Entity Framework #2
- Entity cotains properties
- last name, first name, etc
- Has a special key property
- makes data unique
- May have more than 1 key property, but needs at least 1
- Similar to classes in C#
- Properties may hold simple and complex data
- Relationship between two entities through association
Associations
- Association types define the specifics of the association
- Similar to a DB level join operation
- 1+ properties in each entity (association endpoints) define the relationship between two entities
- Properties may define both single and multicolumn connections between entities (1-to-1, 1-to-many, many-to-many)
- Associations are bidirectional
- May exist although data lacks DB join specification
- Association set: all association instances used to define an association
Associations #2
- Navigation property: allows one entity to view the data provided by an associated entity
- Allows to create a view of an entity from the perspective of another entity
- E.g. Access all orders submitted by that customer (foreign key)
- Derived entities
- Customer can create an order
- Derived entities exist in the same container as the order entity
Entity Container
- Holds all of the entity information together
- App creates entity container instance (context)
- App accesses the entities within a particular context
Entity Framework Elements
- relies on XML
- These files perform 3 tasks
- Define conceptual model
- Define storage model
- Create mappings between models and physical DB
- Framework lets developer interact with the DB using one of 3 techniques
Entity Framework Approaches
- DB first
- Entity framework creates classes that reflect an existing DB design
- Design first:
- Define model of DB, Entity Framework creates DB on server
- Code first:
- Create app, Entity framework creates a model to create the DB later
Conceptual Model
- Define how DB looks from app perspective
- E.g.: C# uses Int32, underlying datatype maybe SQL int type
- Conceptual model may refer to it as Int32, actual physical type maybe different
- Create classes used to interact with DB
- As the conceptual model changes
- Entity framework automatically tracks changes
- Conceptual model defines namespaces (similar to C# namespaces)
- Mainly consists of entity and association definitions used to create the view of the DB
Storage Model
- Part of Entity framework that defines how the DB looks from the DB manager's perspective
- Similar to logical model
- Provides logical view on DB that ultimately translates to physical DB
- Also consists of entity and association definitions
- Storage model includes actual DB data such as commands used to query the info within the DB
- This info is used by ADO.NET to create connection and command objects automatically
Model Mappings
- In order to make the two models work together, the Entity framework requires model mappings
- These three models are stored in separate XML files
- In Visual Studio 2012 there is a single .EDMX file
- .CSDL contains XML for the conceptual model of the DB
- .SSDL defines storage model
- .MSL file creates relation between .CSDL and .SSDL file
- Model mapping defines which conceptual model property translates to particular storage model property
Basic Example
- Model first approach
- Install SQL Server Express 2014
- Install SQL Management Studio
- Create WPF App
- VIEW - OTHER WINDOWS - Data Sources
- ADD New Data Source
- Choose DB Data Source Type
- Choose Entity Data Model DB Model
- Choose Entity Model Content
- View Toolbox - Add Entity
- Right Click - Validate - Generate DB from model
References
Thank you for your attention!
Entity Framework Basics
By dinony
Entity Framework Basics
- 275