Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic...

23
presenta www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 1 Data application made easy with Entity Framework 7 Giancarlo Lelli – Analyst @ Avanade Italy srl Community Lead @ Italian Developer Connection Microsoft MVP: Windows Platform Development @itsonlyGianca – [email protected]

Transcript of Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic...

Page 1: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

presenta

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 1

Data application made easy with Entity Framework 7Giancarlo Lelli – Analyst @ Avanade Italy srl

Community Lead @ Italian Developer Connection

Microsoft MVP: Windows Platform Development

@itsonlyGianca – [email protected]

Page 2: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Agenda• What is EF7?

• Getting started on the new platforms

• Demo, Demo, Demo, ecc…

www.wpc2015.it – [email protected] - +39 02 365738.11 2

Page 3: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

What is Entity Framework 7?

www.wpc2015.it – [email protected] - +39 02 365738.11 3

Page 4: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 4

New PlatformsNew Data Stores

01010001001011000100

Page 5: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

New Platforms

www.wpc2015.it – [email protected] - +39 02 365738.11 5

Page 6: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

New Data Stores

Relational & non-relational

• Not a magic abstraction

• High level services that are useful on all/most stores

• Non-common concerns handled by provider extensions

Example providers

• Relational (SQL Server, SQLite, Postgres, etc.)

• Azure Table Storage

• Redis

• In Memory (for testing)

www.wpc2015.it – [email protected] - +39 02 365738.11 6

Page 7: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Challenges with current code base

• Long history going back ~15 years Older APIs and design patterns

Uses APIs not available on all platforms

Seldom used code/features

Monolithic implementation

Unintuitive behaviors throughout code

• Not optimized for density/devices High memory footprint

• Tight coupling to relational concepts

Page 8: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Part v7 and part v1

• Same top level experience as EF6.x Still DbContext/DbSet etc.

Built over a new lightweight and extensible core

• Just the commonly used features Plus many new features

• Code-based modelling only Still supports creating model from existing database

Page 9: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

9

EF6var metadata = ((IObjectContextAdapter)context).ObjectContext

.MetadataWorkspace;

var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

var entityType = metadata.GetItems<EntityType>(DataSpace.OSpace).Single(e => objectItemCollection.GetClrType(e) == typeof(Blog));

var entitySet = metadata.GetItems<EntityContainer>(DataSpace.CSpace).Single().EntitySets.Single(s => s.ElementType.Name == entityType.Name);

var mapping =metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpace)

.Single().EntitySetMappings

.Single(s => s.EntitySet == entitySet);

var table = mapping.EntityTypeMappings.Single().Fragments.Single().StoreEntitySet;

var tableName = (string)table.MetadataProperties["Table"].Value?? table.Name;

EF7var table = context.Model.GetEntityType(typeof(Blog)).Relational().Table

Finding class-to-table mapping

Page 10: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Moving to EF7

• There are certain features and development approaches thatare being deprecated. The most notable is support for EDMX-based development, which

is sometimes referred to as “model first” or “designer based.”

• Going forward, EF7 and beyond will only support code-basedmodeling, also known as “code first,” but which also includesreverse engineering code first entities from an existingdatabase.

• EF6 is mature, feature-rich, stable and performant. Because ofthat, it’s not going away anytime soon, and the team iscontinuing to fix defects and add minor features.

• It also might take a while for providers other than SQL Server,such as Oracle, to release versions that are compatible with EF7.

www.wpc2015.it – [email protected] - +39 02 365738.11 10

Page 11: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

EF7 scenarios

• ASP.NET 5, EF7 was re-written from the ground up mainly to support this scenario.

• EF6 won’t support CoreCLR and you’ll have to use EF7 if you want to take advantage of the version of .NET that targets the cloud.

• Apps that use EF with code-based modeling (aka Code First), where you don’t use features not yet supported by EF7. The bonus here is that EF7 is deployed as a set of Portable Class

Libraries that can run on .NET 4.5.1, Windows 8.1 and Windows Phone 8.1 (and by extension on iOS and Android via Xamarin). There is also an In-Memory provider that is useful for testing scenarios.

• You want to target non-relational data stores, such as Azure Table Storage or Redis;

• You want to target a local SQLite database on a mobile device;

www.wpc2015.it – [email protected] - +39 02 365738.11 11

Page 12: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

DEMOEntity Framework 7 – UnicornStore

Page 13: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Getting started on the UWP

www.wpc2015.it – [email protected] - +39 02 365738.11 13

Page 14: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

www.wpc2015.it – [email protected] - +39 02 365738.11 14

• The latest pre-release version of EF7 available as of today is EF 7.0.0-rc1-final. https://www.myget.org/F/aspnetvnext/

• To use EF7 you install the package for the database provider(s) you want to target.

> Install-Package EntityFramework.SQLite –Pre

• Then you have to install the commands package as well.

> Install-Package EntityFramework.Commands –Pre

Install Entity Framework

Page 15: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

WARNING!

You can query using simple LINQ operators that do not change the type ofresults returned by the query

• Where

• OrderBy

• Distinct

• Skip/Take

• ToList/ToArray

You cannot use operators that would change the type of results returned by the query.

• Select (not an anonymous type)

• GroupBy

• Include/ThenInclude

• Join

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 15

Page 16: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

WARNING#2

To work around an issue with EF7 and .NET Native, you need to add a runtime directive to your application. This issue will be fixed for future releases.

• Open Properties/Default.rd.xml

• Add the highlighted line shown below

<!-- Add your application specific runtime directives here. -->

<Type Name="System.Collections.ArrayList" Dynamic="Required All" />

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 16

Page 17: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

DEMONew Platforms & Data stores

Page 18: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Writing a custom EF7 provider

www.wpc2015.it – [email protected] - +39 02 365738.11 18

Page 19: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

The basics

• An EF provider must implement a set of APIs tomanage reading from and writing to a persistentdata store. Using these Core APIs, it is possible tocreate a provider for additional data store types.

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 19

Page 20: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Requirements

EF providers must implement the following abstract classes.

• Infrastructure.DbContextOptionsExtension

• Storage.DataStore

• Storage.DataStoreConnection

• Storage.DataStoreCreator

• Storage.DataStoreSource

Optional classes to override.

• Infrastructure.Database

• Infrastructure.ValueGeneratorCache

(All reference names are relative to Microsoft.Data.Entity)

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 20

Page 21: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Recommended

To help end-users discover provider-specific features viaIntelliSense, it is recommend to provide extension methods onsome of the default EF classes.

• DbContextOptions

• EntityServicesBuilder

• Storage.Database

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 21

Page 22: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 22

https://github.com/aspnet/EntityFramework/issues/3936

Page 23: Data application made easy with Entity Framework 7 · Relational & non-relational •Not a magic abstraction •High level services that are useful on all/most stores •Non-common

Contatti OverNetEducation

OverNet [email protected]

www.overneteducation.it

Tel. 02 365738

@overnete

www.facebook.com/OverNetEducation

www.linkedin.com/company/overnet-solutionswww.wpc2015.it

www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 23