.NET Database Technologies: Entity Framework additional notes – part 2.

Slides:



Advertisements
Similar presentations
Satisfy Your Technical Curiosity ADO.NET vNext The Entity Data Model, Object Services, and LINQ Bob Beauchemin Director of Developer Resources, SQLskills.
Advertisements

Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
.NET 3.5 SP1 New features Enhancements Visual Studio 2008 SP1 New features Enhancements Additional features/enhancements.
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
Shyam Pather Development Manager Microsoft Session Code: DTL402.
.NET Database Technologies: Open-Source Frameworks.
Jeff Derstadt Senior Development Lead Microsoft Corporation Patterns & Architecture.
Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
ORM Technologies and Entity Framework (EF)
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Virtual techdays INDIA │ august 2010 Building ASP.NET applications using SQL Server Compact Chaitanya Solapurkar │ Partner Technical Consultant,
Introduction to ADO Entity Framework ir Denis VOITURON Source:
1 Entity Framework Introduction. Outline Goals of Entity Framework 2.
Introduction to MVC Adding Model Classes NTPCUG Tom Perkins, Ph.D.
Entity Framework Code First End to End
LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Entity Framework, a quickstart Florin−Tudor Cristea, Microsoft Student Partner.
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during WakeUpAndCode.com.
Eric Nelson (or )
Visual Studio 2010 and.NET Framework 4 Training Workshop.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Entity Framework Overview. Entity Framework A set of technologies in ADO.NET that support the development of data-oriented software applications A component.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Domain and Persistence Patterns. Fundamental Pattern Types Design Patterns Business Logic Patterns.
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant.
Oct * Brad Tutterow. VS 2008.NET 3.5LINQ Entity Framework  The ADO.NET Entity Framework is part of Microsoft’s next generation of.NET technologies.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
Object Oriented Software Development
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
© 2008 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice Lesson # 9 HP UCMDB 8.0 Essentials.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
® IBM Software Group © 2007 IBM Corporation Module 1: Getting Started with Rational Software Architect Essentials of Modeling with IBM Rational Software.
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Magenic, Principal Consultant Level: Introductory.
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
ADO.NET 3.0 – Entity Data Model Gert E.R. Drapers Chief Software Architect Visual Studio Team Edition for Database Professionals Microsoft Corporation.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Stuart Leitch “Code First” & DbContext.
2 Behind every great site, there is great data Eric Nelson Developer Evangelist Microsoft UK
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
1 Copyright © 2008, Oracle. All rights reserved. Repository Basics.
Template Package  Presented by G.Nagaraju.  What is Template Package?  Why we use Template Package?  Where we use Template Package?  How we create.
Top 10 Entity Framework Features Every Developer Should Know
Introduction to Entity framework
Introduction to Entity Framework
Lesson # 9 HP UCMDB 8.0 Essentials
EF Relations Object Composition
Entity Framework: Code First
Entity Framework DB From Code, OOP Introduction
Best Practices and Architecture
 .NET CORE
Entity Framework By: Casey Griffin.
Learn. Imagine. Build. .NET Conf
Entity Framework 4 and WCF Data Services 4
ADO.NET Entity Framework Marcus Tillett
ADO.NET Entity Framework
Module 1: Getting Started
An Introduction to Entity Framework
.NET Database Technologies:
Entity Framework Core.
Accessing Data in a .NET Web Application
IT College 2016, Andres käver
Entity Framework Core for Enterprise Applications
ADO.NET Entity Framework
Presentation transcript:

.NET Database Technologies: Entity Framework additional notes – part 2

EF1 => EF4 “The 1.0 release of the Entity Framework was not a resounding success – developers found themselves fighting the framework” EF4 gives you the choice of how to work Database-first, model-first, code-first Supports POCO entities for Persistence Ignorance

POCO support EF1 entity classes inherit from EntityObject Difficult to separate concerns in application  Classes are difficult to unit test  For unit testing, want to “fake” persistent storage Difficult to switch DAL technology  Can’t take existing classes and use EF1  Need to redesign classes significantly to bind them to EF EF4 allows you to use POCOs Model classes can be completely ignorant of persistence mechanism

POCO classes POCO classes must work in conjunction with EDM Must mirror entities defines in EDM However, EDM does not generate the classes  Set Code Generation Strategy to none in designer properties Two ways to create classes  Write POCO code by hand  Use a T4 Template Microsoft ADO.NET C# POCO Entity Generator Download from Right-click EDM designer surface and select Add Code Generation Item Creates template for generating POCOs to match EDM

T4 Text Template Transformation Toolkit Code generation tool built into Visual Studio Can be used in many different code generation scenarios Template file has.tt extension When a project has a.tt file, the template is processed at build time Generated code file(s) associated with template in Solution Explorer, but can be moved elsewhere if required

Managing POCOs Need to create a custom ObjectContext class Should have an ObjectSet for each entity type Needs to know name of EDMX file (passed in as a constructor parameter) to allow it to access mapping and storage information

Issues with POCOs – change tracking Change tracking Classes derived from EntityObject constantly communicate with ObjectContext POCOs can’t do this ObjectContext has to track changes to POCOs:  DetectChanges method  SaveOptions -> DetectAllChanges forces call to DetectChanges before saving  Default behaviour

Issues with POCOs – loading related data EntityObject can load related data with Load method  e.g. employee.AddressesReference.Load()  Navigation property also has an EntityReference property POCO does not have EntityReference properties  Need to load related data from context  e.g. context.LoadProperty ( employee, e => e.Addresses) Can also support lazy loading with POCOs through dynamic proxies (see later)

Issues with POCOs – two-way relationships Context forces EntityObject classes to be aware of two- way relationships and keeps relationships in sync Options for doing this with POCOs:  Call DetectChanges method of context  Write code in POCO classes so that they manage their own relationships  Use dynamic proxies

Dynamic proxies POCO classes can be wrapped by proxy classes which are created dynamically at runtime Proxy class is derived from POCO class POCO properties must be marked as virtual Adds EF-specific behaviour which allows management of change tracking, lazy-loading and relationships in the same way as EntityObject classes Client code works with POCO classes, but actual objects instantiated are proxies EF uses reflection to find out about properties of POCOs and Reflection.Emit to dynamically generate IL code for proxy classes

POCOs and PI POCOs do not need to derive from any persistence specific class Do not use persistence specific collection types  Should use ICollection as collection property type Requirement for properties to be virtual to support some capabilities through proxies  This does not violate PI – classes don’t need to know about what the derived classes will do EF4 POCO support allows PI domain classes to be used

Using a repository Rather than having client code query context directly it is common to use a repository A repository “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects” (Fowler) Promotes testability  Can swap in fake context with affecting client/test code Can program client to an interface for repository and define actual repository type through dependency injection  Allows DAL to be swapped easily Context implements Unit of Work pattern, but often wrap this in UoW class/interface to allow further isolation of dependencies

IoC and Dependency Injection Inversion of Control (IoC)  Objects do not create other objects on which they rely to do their work  Instead, they get the objects that they need from an outside source (for example, an xml configuration file) Dependency Injection (DI)  This is done without the object intervention, usually by a container that passes constructor parameters and set properties Castle Windsor is an example of a.NET DI container Can manage dependencies for all kinds of service, not just persistence

Creating a repository Pattern describes solution to problem, but does not strictly define how to write the code Many variations on the Repository pattern are possible You can design your own – there are plenty of articles and blog posts with examples to base it on You can use a template to generate your basic implementation from an EDM  ADO.NET Unit Testable Repository Generator Download from  EFRepository Download from efrepository.codeplex.com

Model-First All examples so far have been re-engineered from the database Changes to the conceptual model were mapped to the original database EF4 allows you to start by designing the conceptual model and then use that model to define the database Model-First

Creating the conceptual model Add new ADO.NET Entity Data Model item to project and select Empty Data Model in wizard Add new Entities to model using Toolbox or right-click on designer surface Add properties (scalar or complex) to each entity by right- clicking on entity  Set type of property in Properties window  Give each entity a key property Add associations by right-clicking on surface or entities  Adding association creates navigation properties Create inheritance hierarchy by specifying base class for new entities

Generate database Right-click on designer surface and select Generate Database from the Model Slightly misleading name  Database must exist – designer will generate DDL to add to database  Designer does not execute the DDL script it creates – have to do that separately DDL Generation wizard allows you to specify the database, and creates connection string in app.config Wizard adds SSDL and MSL portions to EDMX file and creates SQL script in project folder

DDL generation Based on a T4 template Table names come from EntitySet names Property and association attributes set in designer will be reflected in column attributes, e.g. data types, cascade delete Inheritance will be implemented with a TPT strategy Many-to-many relationship implemented with a join table

Customising DDL generation Possible to modify T4 template Only template installed in VS2010 is SSDL to SQL 10.tt Or use Entity Designer Database Generation Power Pack  Download from  Contains additional templates  Can force TPH strategy for inheritance  Provides capability to migrate existing database after making changes to model

Model-First and POCOs Model-First approach is compatible with POCOs Once EDMX has been created, you can create POCOs to mirror entities and ObjectContext class to manage them Alternatively, the entities you create in the designer can be based on knowledge of existing POCO classes

Code-First Avoid altogether working with visual model Build your model with POCO classes Moving towards true Domain Driven Design Microsoft consulted with a Data Programmability Advisory Council which included Fowler, Evans and Nilsson Currently this feature is a CTP, not included in the release of VS2010  Need to download and install  Add reference to Microsoft.Data.Entity.Ctp.dll which is installed in Program Files\Microsoft ADO.NET Entity Framework Feature CTP5\Binaries  Add using System.Data.Entity where required

What does Code-First do? You need to create POCOs and a context class which inherits from DbContext Defines DbSet property to store a set of each entity class At runtime it reads your class definitions and creates in- memory representation of CSDL, MSL and SSDL Looks for connection string that has the same name as the context class Uses convention-based persistence mapping Convention over configuration Creates database if it doesn’t already exist (SQL CE and SQL Express)

Customising mapping Override OnModelCreating event of DbContext and use ModelBuilder class to customise mapping  Change table names  Customise column/property mapping  Split a table across multiple types  framework-4-code-first-custom-database-schema- mapping.aspx Data annotation attributes  Specify keys, string lengths, etc  annotations-in-the-entity-framework-and-code-first.aspx

EF4 options - summary Database-First  Define model and mappings in EDMX  Entities are EntityObjects or POCOs Model-First  Define model in EDMX  Entities are EntityObjects or POCOs Code-First  Define model with POCOs  Mappings defined by convention or ModelBuilder/annotations  Entities are POCOs

Profiling EF Profile at server  SQL Server Profiler Profile at client  EF Profiler   Not free!