Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
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]
Agenda• What is EF7?
• Getting started on the new platforms
• Demo, Demo, Demo, ecc…
www.wpc2015.it – [email protected] - +39 02 365738.11 2
What is Entity Framework 7?
www.wpc2015.it – [email protected] - +39 02 365738.11 3
www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 4
New PlatformsNew Data Stores
01010001001011000100
New Platforms
www.wpc2015.it – [email protected] - +39 02 365738.11 5
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
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
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
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
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
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
DEMOEntity Framework 7 – UnicornStore
Getting started on the UWP
www.wpc2015.it – [email protected] - +39 02 365738.11 13
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
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
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
DEMONew Platforms & Data stores
Writing a custom EF7 provider
www.wpc2015.it – [email protected] - +39 02 365738.11 18
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
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
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
www.wpc2015.it – [email protected] - +39 02 365738.11 - #wpc15it 22
https://github.com/aspnet/EntityFramework/issues/3936
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