SlideShare a Scribd company logo
#sqlsatParma
#sqlsat355November 22nd, 2014
Entity Framework 6 for developers
Code-First
Michael Denny
@dennymic
about.me/micdenny
#sqlsatParma
#sqlsat355November 22nd, 2014
Sponsors
#sqlsatParma
#sqlsat355November 22nd, 2014
Organizers
#sqlsatParma
#sqlsat355November 22nd, 2014
Speaker info
 Microsoft Visual C# MVP
 Blogs:
 [ITA] http://blogs.dotnethell.it/regulator/
 [ENG] http://dennymichael.net/
 Community/Forum:
 http://www.dotnethell.it
 Twitter: @dennymic
 More details on:
 http://about.me/micdenny/
#sqlsatParma
#sqlsat355November 22nd, 2014
Agenda
 Concetti ORM
 Scegliere tra designer o codice
 Entity Framework Code-First in azione
 Evolvere il database: Code-First Migrations
 Importare un database esistente
 Strumenti di Logging e Profiling
 What’s Next
 Q&A
#sqlsatParma
#sqlsat355November 22nd, 2014
Visual Studio Community 2013
 Include tutte le funzionalità di Visual Studio Professional 2013
 Sviluppatori indipendenti per applicazioni sia free che a pagamento, studenti,
collaboratori open source, e organizzazioni con team fino a 5 persone
#sqlsatParma
#sqlsat355November 22nd, 2014
Concetti ORM
 Integrazione tra OOP e RDBMS
 Persistenza dei dati con interfaccia
unificata
 Interfaccia orientata agli oggetti
 Astrazione sul tipo di database utilizzato
 Sviluppo nel vostro linguaggio (C# )
 Riduzione di codice ripetitivo CRUD
 Codice più «pulito» e ben strutturato
#sqlsatParma
#sqlsat355November 22nd, 2014
Entity Framework
 Implementazione ORM open-source per
ADO.NET
 Librerie su NuGet
 Lazy-Load
 Caching (object, query plan, metadata)
 Gestione della Concorrenza
 Repository Pattern (DbSet)
 Unit of Work (DbContext)
 http://msdn.microsoft.com/data/ee712907
#sqlsatParma
#sqlsat355November 22nd, 2014
Architettura EF
#sqlsatParma
#sqlsat355November 22nd, 2014
Database
Esistente
Database First
• Creazione modello partendo da un
DB esistente (Reverse Engineering)
• Classi auto-generate dal modello
Code First (Database Esistente)
• Definizione classi da codice
• Strumenti per l’auto-generazione delle
classi partendo da un DB esistente
(Reverse Engineering)
Nuovo
Database
Model First
• Creazione modello da designer
• Database creato dal modello
• Classi auto-generate dal modello
Code First (Nuovo Database)
• Definizione classi da codice
• Database creato dal modello
• Possibilità di evolvere il database
utilizzando le Migrations
Designer o Codice
#sqlsatParma
#sqlsat355November 22nd, 2014
EF Code-First in azione
 Modellazione dati
 Convenzioni
 Operazioni basilari (CRUD)
 Query LINQ
 Personalizzazioni con Data Annotation
 Personalizzazioni con Fluent API
#sqlsatParma
#sqlsat355November 22nd, 2014
Modellazione dati
 POCO class
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
#sqlsatParma
#sqlsat355November 22nd, 2014
Modellazione dati
 Le proprietà di navigazione definiscono il tipo di relazione
tra le entità in base al tipo di ritorno:
 Riferimento, per relazioni uno a uno
 Collezione, per relazioni uno a molti
 Includere sempre anche la foreign
key sulla classe che rappresenta
l’oggetto dipendente
 Virtual abilita il Lazy Loading
vedi: http://msdn.microsoft.com/data/jj574232
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts
{ get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
#sqlsatParma
#sqlsat355November 22nd, 2014
DbContext e DbSet
 Responsabile dell’interazione con il database
 In grado di creare il database dal modello classi
 Gestisce e popola gli oggetti entità
 Rileva le modifiche (change tracking)
 Persiste le informazioni a database
 Implementa la Unit of Work (SaveChanges)
 Espone proprietà DbSet (Repository Pattern) che
rappresentano collezioni di specifiche entità
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
#sqlsatParma
#sqlsat355November 22nd, 2014
DEMO 01
 Modellazione con POCO class
 DbContext
 DbSet
 Creazione database
#sqlsatParma
#sqlsat355November 22nd, 2014
Convenzioni
 System.Data.Entity.ModelConfiguration.Conventions
 Chiave primaria
 Relazioni
 Connection string
 Rimuovere una convenzione
 Convenzioni personalizzate
#sqlsatParma
#sqlsat355November 22nd, 2014
Convenzioni: Chiave Primaria
 Nome proprietà "Id" (non case sentitive)
 Nome classe seguito da "Id", es.: "UserId"
 Identity Column se il tipo è numerico o GUID
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
#sqlsatParma
#sqlsat355November 22nd, 2014
 Basate sulle proprietà di navigazione
 Foreign key deve avere lo stesso tipo dato della chiave
primaria e nome:
1. «nome proprietà di navigazione»«nome proprietà chiave primaria» es.:
BlogBlogId
2. «nome classe principale»«nome proprietà chiave primaria» es.:
BlogBlogId
3. «nome proprietà chiave primaria»
es.: BlogId
 Non è case sensitive
 FK nullable = relazione opzionale
 Cascade delete se FK != nullable
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
}
Convenzioni: Relazioni
#sqlsatParma
#sqlsat355November 22nd, 2014
Convenzioni: Connection String
 DbContext() = Code-First + IConnectionFactory:
 Nome db: «namespace».«nome classe dbcontext>
 Server: SQL Express oppure LocalDb, se entrambi sono
installati, viene utilizzato SQL Express
 DbContext("MyDatabase") = Code-First +
IConnectionFactory:
 Nome db: MyDatabase
 Server: SQL Express o LocalDb
 Se nell’app.config esiste una connection string con lo stesso
nome, allora viene usata quella e bypassato il factory
 DbContext("name=MyDatabase") = Code-First:
 Forza la dichiarazione della connection string su app.config
#sqlsatParma
#sqlsat355November 22nd, 2014
Convenzioni: Personalizzazioni
 Rimuovere una convenzione
 Convenzioni personalizzate
public class BloggingContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class BloggingContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<int>()
.Where(t => t.Name.StartsWith("id", StringComparison.InvariantCultureIgnoreCase))
.Configure(t => t.IsKey().HasColumnOrder(1));
}
}
#sqlsatParma
#sqlsat355November 22nd, 2014
Convenzioni
 IdKeyDiscoveryConvention
 Rileva le proprietà per la chiave primaria
 PluralizingTableNameConvention
 Nome tabella = plurale nome classe
 PluralizingEntitySetNameConvention
 Proprietà di navigazione = plurale nome tipo dato
 DecimalPropertyConvention
 Setta la precision e la scale (18,2) per le proprietà decimali
 Tanti tanti altri...
 Vedi: System.Data.Entity.ModelConfiguration.Conventions
#sqlsatParma
#sqlsat355November 22nd, 2014
Operazioni basilari (CRUD)
using (var db = new BloggingContext())
{
var blog = new Blog();
blog.Name = "Il mio primo blog";
db.Blogs.Add(blog);
db.SaveChanges();
}
using (var db = new BloggingContext())
{
var blog = db.Blogs.Find(1);
Console.WriteLine(blog.Name);
}
using (var db = new BloggingContext())
{
var blog = db.Blogs.Find(1);
blog.Name = "Nome modificato!!";
db.SaveChanges();
}
using (var db = new BloggingContext())
{
var blog = db.Blogs.Find(1);
db.Blogs.Remove(blog);
db.SaveChanges();
}
#sqlsatParma
#sqlsat355November 22nd, 2014
DEMO 02
 Create
 Read
 Update
 Delete
#sqlsatParma
#sqlsat355November 22nd, 2014
 LINQ Query
 Lambda Expression
Query LINQ: Filtrare
using (var db = new BloggingContext())
{
var results = from post in db.Posts
where post.Title.Contains("trovami")
select post;
foreach (var result in results)
Console.WriteLine(result.Title);
}
using (var db = new BloggingContext())
{
var results = db.Posts
.Where(post => post.Title.Contains("trovami"));
foreach (var result in results)
Console.WriteLine(result.Title);
}
SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[Title] AS [Title],
[Extent1].[Content] AS [Content],
[Extent1].[BlogId] AS [BlogId]
FROM
[dbo].[Posts] AS [Extent1]
WHERE
[Extent1].[Title] LIKE N'%trovami%'
#sqlsatParma
#sqlsat355November 22nd, 2014
Query LINQ: Ordinare
using (var db = new BloggingContext())
{
var results = from post in db.Posts
orderby post.Title
select post;
foreach (var result in results)
Console.WriteLine(result.Title);
}
using (var db = new BloggingContext())
{
var results = db.Posts
.OrderBy(t => t.Title);
foreach (var result in results)
Console.WriteLine(result.Title);
}
using (var db = new BloggingContext())
{
var results = db.Posts
.Where(t => t.Content.Contains("trovami"))
.OrderBy(t => t.Title);
foreach (var result in results)
Console.WriteLine(result.Title);
}
SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[Title] AS [Title],
[Extent1].[Content] AS [Content],
[Extent1].[BlogId] AS [BlogId]
FROM
[dbo].[Posts] AS [Extent1]
ORDER BY
[Extent1].[Title] ASC
SELECT
[Extent1].[PostId] AS [PostId],
[Extent1].[Title] AS [Title],
[Extent1].[Content] AS [Content],
[Extent1].[BlogId] AS [BlogId]
FROM
[dbo].[Posts] AS [Extent1]
WHERE
[Extent1].[Content] LIKE N'%trovami%'
ORDER BY
[Extent1].[Title] ASC
LINQ Query OrderBy
Lambda Expression OrderBy
Lambda Expression Where + OrderBy
#sqlsatParma
#sqlsat355November 22nd, 2014
Query LINQ: Join(are)
 Esplicite
 Implicite
using (var db = new BloggingContext())
{
var results = from blog in db.Blogs
join post in db.Posts on blog.BlogId equals post.BlogId
select new
{
BlogName = blog.Name,
PostTitle = post.Title
};
foreach (var result in results)
Console.WriteLine("blog: {0} post: {1}", result.BlogName, result.PostTitle);
}
using (var db = new BloggingContext())
{
var results = from blog in db.Blogs
from post in blog.Posts
select new
{
BlogName = blog.Name,
PostTitle = post.Title
};
foreach (var result in results)
Console.WriteLine("blog: {0} post: {1}", result.BlogName, result.PostTitle);
}
SELECT
[Extent1].[BlogId] AS [BlogId],
[Extent1].[Name] AS [Name],
[Extent2].[Title] AS [Title]
FROM
[dbo].[Blogs] AS [Extent1]
INNER JOIN
[dbo].[Posts] AS [Extent2]
ON [Extent1].[BlogId] = [Extent2].[BlogId]
#sqlsatParma
#sqlsat355November 22nd, 2014
Query: Stored Procedure
 Raw SQL Queries
http://msdn.microsoft.com/data/jj592907
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public Blog GetBlogById(int blogId)
{
return this.Blogs
.SqlQuery("proc_GetBlogById @p0", blogId)
.FirstOrDefault();
}
public Blog GetBlogById2(int blogId)
{
return this.Blogs
.SqlQuery("proc_GetBlogById @BlogId",
new SqlParameter("@BlogId", blogId))
.FirstOrDefault();
}
}
#sqlsatParma
#sqlsat355November 22nd, 2014
Strumenti di Logging
 Proprietà Log a delegato
 Personalizzazione del contenuto e del formato
 Sistema di intercettazione a moduli per
maggiore controllo e flessibilità
 Gli Interceptor possono essere aggiunti da
config senza dovere ricompilare il progetto
BloggingContext.Database.Log = Console.Write;
#sqlsatParma
#sqlsat355November 22nd, 2014
DEMO 03
 Filtrare
 Ordinare
 Join(are)
 Stored Procedure
 Logging
#sqlsatParma
#sqlsat355November 22nd, 2014
Evolvere il database: Code-First Migrations
 Sempre sotto il nostro controllo
 Personalizzazioni (anche estreme) da
codice
 Semplicità di utilizzo
 Enable-Migrations
 Add-Migration
 Update-Database
 http://msdn.microsoft.com/data/jj591621
#sqlsatParma
#sqlsat355November 22nd, 2014
DEMO 04
 Enable-Migrations
 Add-Migrations
 Update-Database
 Migrate.exe
packagesEntityFramework.6.1.1tools
#sqlsatParma
#sqlsat355November 22nd, 2014
Importare un database esistente
 Da zero senza reverse engineering
 Con strumenti di automazione:
 Entity Framework Tools (incluso in VS2013)
www.microsoft.com/download/details.aspx?id=40762
 Entity Framework Power Tools (NuGet)
 EntityFramework Reverse POCO Generator (NuGet)
 Code First Migrations anche per database
esistenti/importati
http://msdn.microsoft.com/data/dn579398
#sqlsatParma
#sqlsat355November 22nd, 2014
DEMO 05
 Importare un database esistente
 Entity Framework Tools
 Entity Framework Power Tools
 EntityFramework Reverse POCO Generator
#sqlsatParma
#sqlsat355November 22nd, 2014
Strumenti di Profiling
 SQL Server Profiler (Microsoft)
 SQL XEvent Profiler (free Idera)
 EF Profiler (commerciale, installer
HibernatingRhinos.com)
 ORM Profiler (commerciale, NuGet)
 LINQ Insight Express (free, NuGet)
#sqlsatParma
#sqlsat355November 22nd, 2014
What’s Next
 Versione 7 più leggera ed estensibile
 Sarà disponibile per nuove piattaforme (Store
App, Phone App, Net Core, Mac, Linux)
 Abiliterà nuovi data store, come sistemi non-
relazionali
 Il passaggio a EF7 sarà semplificato il più
possibile (DbContext e DbSet saranno presenti)
 Sviluppato e disponibile in pre-alpha su GitHub
https://github.com/aspnet/EntityFramework
#sqlsatParma
#sqlsat355November 22nd, 2014
In pillole…
 Include(t => t.Posts) evita il lazy loading
http://msdn.microsoft.com/data/jj574232
 Find(1) ricerca per chiave
http://msdn.microsoft.com/data/jj573936
 Entity<>.ToTable("Posts", "Blog")
per creare Posts nello schema Blog
 Async Query & Save
http://msdn.microsoft.com/data/jj819165
 CRUD con Stored Procedure
http://msdn.microsoft.com/data/dn468673
#sqlsatParma
#sqlsat355November 22nd, 2014
In pillole…
 Considerazioni sulle performance
http://msdn.microsoft.com/data/hh949853
 Migliorare lo startup dell’applicazione con NGen
http://msdn.microsoft.com/data/dn582034
 Pre-generare le Mapping Views (EDM) per
velocizzare dell’inizializzazione del DbContext
http://msdn.microsoft.com/data/dn469601
 Gestione della concorrenza ottimistica
http://msdn.microsoft.com/data/jj592904
#sqlsatParma
#sqlsat355November 22nd, 2014
In pillole…
 Migrate.exe per controllare e automatizzare il
processo di migrazione del database
http://msdn.microsoft.com/data/jj618307
 AsNoTracking()
http://msdn.microsoft.com/data/jj556203
 Disabilitare il tracking automatico
http://msdn.microsoft.com/data/jj556205
 Unit Test: testare isolandosi dal database
http://msdn.microsoft.com/data/dn314429
#sqlsatParma
#sqlsat355November 22nd, 2014
Q&A
 Questions?
#sqlsatParma
#sqlsat355November 22nd, 2014
THANKS!
#sqlsatParma
#sqlsat355

More Related Content

Viewers also liked

Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5
Ehtsham Khan
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
Entity Framework Core & Micro-Orms with Asp.Net Core
Entity Framework Core & Micro-Orms with Asp.Net CoreEntity Framework Core & Micro-Orms with Asp.Net Core
Entity Framework Core & Micro-Orms with Asp.Net Core
Stephane Belkheraz
 
Feedback Loops...to infinity, and beyond!
Feedback Loops...to infinity, and beyond!Feedback Loops...to infinity, and beyond!
Feedback Loops...to infinity, and beyond!
Rui Carvalho
 
MVC 6 - Tag Helpers and View Components
MVC 6 - Tag Helpers and View ComponentsMVC 6 - Tag Helpers and View Components
MVC 6 - Tag Helpers and View Components
David Paquette
 
ADO.NET Entity Framework
ADO.NET Entity FrameworkADO.NET Entity Framework
ADO.NET Entity Framework
ukdpe
 
Learn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database FirstLearn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database First
Jibran Rasheed Khan
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
James Johnson
 
Improve your web dev workflow in Visual Studio
Improve your web dev workflow in Visual StudioImprove your web dev workflow in Visual Studio
Improve your web dev workflow in Visual Studio
David Paquette
 
04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core 04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core
Cellenza
 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity Framework
LearnNowOnline
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
Hatim Hakeel
 
Orms vs Micro-ORMs
Orms vs Micro-ORMsOrms vs Micro-ORMs
Orms vs Micro-ORMs
David Paquette
 
State of entity framework
State of entity frameworkState of entity framework
State of entity framework
David Paquette
 
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
Pavel Tsukanov
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
Bishoy Demian
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
Mahmoud Ouf
 
Entity Framework
Entity FrameworkEntity Framework
Entity Framework
vrluckyin
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
Mahmoud Ouf
 

Viewers also liked (20)

Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
Entity Framework Core & Micro-Orms with Asp.Net Core
Entity Framework Core & Micro-Orms with Asp.Net CoreEntity Framework Core & Micro-Orms with Asp.Net Core
Entity Framework Core & Micro-Orms with Asp.Net Core
 
Feedback Loops...to infinity, and beyond!
Feedback Loops...to infinity, and beyond!Feedback Loops...to infinity, and beyond!
Feedback Loops...to infinity, and beyond!
 
MVC 6 - Tag Helpers and View Components
MVC 6 - Tag Helpers and View ComponentsMVC 6 - Tag Helpers and View Components
MVC 6 - Tag Helpers and View Components
 
ADO.NET Entity Framework
ADO.NET Entity FrameworkADO.NET Entity Framework
ADO.NET Entity Framework
 
Learn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database FirstLearn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database First
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Improve your web dev workflow in Visual Studio
Improve your web dev workflow in Visual StudioImprove your web dev workflow in Visual Studio
Improve your web dev workflow in Visual Studio
 
04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core 04 - [ASP.NET Core] Entity Framework Core
04 - [ASP.NET Core] Entity Framework Core
 
Introducing the Entity Framework
Introducing the Entity FrameworkIntroducing the Entity Framework
Introducing the Entity Framework
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
 
Orms vs Micro-ORMs
Orms vs Micro-ORMsOrms vs Micro-ORMs
Orms vs Micro-ORMs
 
State of entity framework
State of entity frameworkState of entity framework
State of entity framework
 
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
ORM технологии в .NET (Nhibernate, Linq To SQL, Entity Framework)
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
 
Entity Framework
Entity FrameworkEntity Framework
Entity Framework
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 

Similar to Entity Framework 6 for developers, Code-First!

Flavio atzeni smau mi 2013
Flavio atzeni smau  mi 2013Flavio atzeni smau  mi 2013
Flavio atzeni smau mi 2013
Paradisi63
 
Django
DjangoDjango
Django
GDG Bologna
 
Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo MicrosoftAsp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
Stefano Benedetti
 
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Matteo Enna
 
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case studyMantenere una distribuzione Drupal attraverso test coverage: Paddle case study
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
DrupalDay
 
Javascript
JavascriptJavascript
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANODrupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
DrupalDay
 
Programmazione web libera dai framework
Programmazione web libera dai frameworkProgrammazione web libera dai framework
Programmazione web libera dai framework
Francesca1980
 
Wcmil2018
Wcmil2018Wcmil2018
Wcmil2018
ErikaGili
 
Session 02 - schema design e architettura
Session 02 - schema design e architetturaSession 02 - schema design e architettura
Session 02 - schema design e architetturaMongoDB
 
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
Tsuneo Kurihara
 
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
azuredayit
 
MongoDB User Group Padova - Overviews iniziale su MongoDB
MongoDB User Group Padova - Overviews iniziale su MongoDBMongoDB User Group Padova - Overviews iniziale su MongoDB
MongoDB User Group Padova - Overviews iniziale su MongoDB
Stefano Dindo
 
jQuery e i suoi plugin
jQuery e i suoi pluginjQuery e i suoi plugin
jQuery e i suoi plugin
Pasquale Puzio
 
JavaScript
JavaScriptJavaScript
JavaScript
Manuel Scapolan
 
Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8
Luca Lusso
 
Yagwto
YagwtoYagwto
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
Codemotion
 
Hands on MVC - Mastering the Web
Hands on MVC - Mastering the WebHands on MVC - Mastering the Web
Hands on MVC - Mastering the Web
Claudio Gandelli
 

Similar to Entity Framework 6 for developers, Code-First! (20)

Flavio atzeni smau mi 2013
Flavio atzeni smau  mi 2013Flavio atzeni smau  mi 2013
Flavio atzeni smau mi 2013
 
Django
DjangoDjango
Django
 
Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo MicrosoftAsp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
 
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
 
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case studyMantenere una distribuzione Drupal attraverso test coverage: Paddle case study
Mantenere una distribuzione Drupal attraverso test coverage: Paddle case study
 
Javascript
JavascriptJavascript
Javascript
 
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANODrupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
 
Programmazione web libera dai framework
Programmazione web libera dai frameworkProgrammazione web libera dai framework
Programmazione web libera dai framework
 
Wcmil2018
Wcmil2018Wcmil2018
Wcmil2018
 
Session 02 - schema design e architettura
Session 02 - schema design e architetturaSession 02 - schema design e architettura
Session 02 - schema design e architettura
 
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
Accelera lo sviluppo con il web framework! Componenti a supporto dello svilup...
 
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
Azure Day Rome Reloaded 2019 - Ingestion nel datalake passando tramite API Ma...
 
MongoDB User Group Padova - Overviews iniziale su MongoDB
MongoDB User Group Padova - Overviews iniziale su MongoDBMongoDB User Group Padova - Overviews iniziale su MongoDB
MongoDB User Group Padova - Overviews iniziale su MongoDB
 
jQuery e i suoi plugin
jQuery e i suoi pluginjQuery e i suoi plugin
jQuery e i suoi plugin
 
Basi Di Dati 05
Basi Di Dati 05Basi Di Dati 05
Basi Di Dati 05
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8
 
Yagwto
YagwtoYagwto
Yagwto
 
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
Go reactive with Realm and Xamarin Forms - Andrea Ceroni - Codemotion Rome 2018
 
Hands on MVC - Mastering the Web
Hands on MVC - Mastering the WebHands on MVC - Mastering the Web
Hands on MVC - Mastering the Web
 

Entity Framework 6 for developers, Code-First!

  • 1. #sqlsatParma #sqlsat355November 22nd, 2014 Entity Framework 6 for developers Code-First Michael Denny @dennymic about.me/micdenny
  • 4. #sqlsatParma #sqlsat355November 22nd, 2014 Speaker info  Microsoft Visual C# MVP  Blogs:  [ITA] http://blogs.dotnethell.it/regulator/  [ENG] http://dennymichael.net/  Community/Forum:  http://www.dotnethell.it  Twitter: @dennymic  More details on:  http://about.me/micdenny/
  • 5. #sqlsatParma #sqlsat355November 22nd, 2014 Agenda  Concetti ORM  Scegliere tra designer o codice  Entity Framework Code-First in azione  Evolvere il database: Code-First Migrations  Importare un database esistente  Strumenti di Logging e Profiling  What’s Next  Q&A
  • 6. #sqlsatParma #sqlsat355November 22nd, 2014 Visual Studio Community 2013  Include tutte le funzionalità di Visual Studio Professional 2013  Sviluppatori indipendenti per applicazioni sia free che a pagamento, studenti, collaboratori open source, e organizzazioni con team fino a 5 persone
  • 7. #sqlsatParma #sqlsat355November 22nd, 2014 Concetti ORM  Integrazione tra OOP e RDBMS  Persistenza dei dati con interfaccia unificata  Interfaccia orientata agli oggetti  Astrazione sul tipo di database utilizzato  Sviluppo nel vostro linguaggio (C# )  Riduzione di codice ripetitivo CRUD  Codice più «pulito» e ben strutturato
  • 8. #sqlsatParma #sqlsat355November 22nd, 2014 Entity Framework  Implementazione ORM open-source per ADO.NET  Librerie su NuGet  Lazy-Load  Caching (object, query plan, metadata)  Gestione della Concorrenza  Repository Pattern (DbSet)  Unit of Work (DbContext)  http://msdn.microsoft.com/data/ee712907
  • 10. #sqlsatParma #sqlsat355November 22nd, 2014 Database Esistente Database First • Creazione modello partendo da un DB esistente (Reverse Engineering) • Classi auto-generate dal modello Code First (Database Esistente) • Definizione classi da codice • Strumenti per l’auto-generazione delle classi partendo da un DB esistente (Reverse Engineering) Nuovo Database Model First • Creazione modello da designer • Database creato dal modello • Classi auto-generate dal modello Code First (Nuovo Database) • Definizione classi da codice • Database creato dal modello • Possibilità di evolvere il database utilizzando le Migrations Designer o Codice
  • 11. #sqlsatParma #sqlsat355November 22nd, 2014 EF Code-First in azione  Modellazione dati  Convenzioni  Operazioni basilari (CRUD)  Query LINQ  Personalizzazioni con Data Annotation  Personalizzazioni con Fluent API
  • 12. #sqlsatParma #sqlsat355November 22nd, 2014 Modellazione dati  POCO class public class Blog { public int BlogId { get; set; } public string Name { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public virtual Blog Blog { get; set; } }
  • 13. #sqlsatParma #sqlsat355November 22nd, 2014 Modellazione dati  Le proprietà di navigazione definiscono il tipo di relazione tra le entità in base al tipo di ritorno:  Riferimento, per relazioni uno a uno  Collezione, per relazioni uno a molti  Includere sempre anche la foreign key sulla classe che rappresenta l’oggetto dipendente  Virtual abilita il Lazy Loading vedi: http://msdn.microsoft.com/data/jj574232 public class Blog { public int BlogId { get; set; } public string Name { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public virtual Blog Blog { get; set; } }
  • 14. #sqlsatParma #sqlsat355November 22nd, 2014 DbContext e DbSet  Responsabile dell’interazione con il database  In grado di creare il database dal modello classi  Gestisce e popola gli oggetti entità  Rileva le modifiche (change tracking)  Persiste le informazioni a database  Implementa la Unit of Work (SaveChanges)  Espone proprietà DbSet (Repository Pattern) che rappresentano collezioni di specifiche entità public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } }
  • 15. #sqlsatParma #sqlsat355November 22nd, 2014 DEMO 01  Modellazione con POCO class  DbContext  DbSet  Creazione database
  • 16. #sqlsatParma #sqlsat355November 22nd, 2014 Convenzioni  System.Data.Entity.ModelConfiguration.Conventions  Chiave primaria  Relazioni  Connection string  Rimuovere una convenzione  Convenzioni personalizzate
  • 17. #sqlsatParma #sqlsat355November 22nd, 2014 Convenzioni: Chiave Primaria  Nome proprietà "Id" (non case sentitive)  Nome classe seguito da "Id", es.: "UserId"  Identity Column se il tipo è numerico o GUID public class Blog { public int BlogId { get; set; } public string Name { get; set; } public virtual ICollection<Post> Posts { get; set; } }
  • 18. #sqlsatParma #sqlsat355November 22nd, 2014  Basate sulle proprietà di navigazione  Foreign key deve avere lo stesso tipo dato della chiave primaria e nome: 1. «nome proprietà di navigazione»«nome proprietà chiave primaria» es.: BlogBlogId 2. «nome classe principale»«nome proprietà chiave primaria» es.: BlogBlogId 3. «nome proprietà chiave primaria» es.: BlogId  Non è case sensitive  FK nullable = relazione opzionale  Cascade delete se FK != nullable public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } public class Blog { public int BlogId { get; set; } } Convenzioni: Relazioni
  • 19. #sqlsatParma #sqlsat355November 22nd, 2014 Convenzioni: Connection String  DbContext() = Code-First + IConnectionFactory:  Nome db: «namespace».«nome classe dbcontext>  Server: SQL Express oppure LocalDb, se entrambi sono installati, viene utilizzato SQL Express  DbContext("MyDatabase") = Code-First + IConnectionFactory:  Nome db: MyDatabase  Server: SQL Express o LocalDb  Se nell’app.config esiste una connection string con lo stesso nome, allora viene usata quella e bypassato il factory  DbContext("name=MyDatabase") = Code-First:  Forza la dichiarazione della connection string su app.config
  • 20. #sqlsatParma #sqlsat355November 22nd, 2014 Convenzioni: Personalizzazioni  Rimuovere una convenzione  Convenzioni personalizzate public class BloggingContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } public class BloggingContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Properties<int>() .Where(t => t.Name.StartsWith("id", StringComparison.InvariantCultureIgnoreCase)) .Configure(t => t.IsKey().HasColumnOrder(1)); } }
  • 21. #sqlsatParma #sqlsat355November 22nd, 2014 Convenzioni  IdKeyDiscoveryConvention  Rileva le proprietà per la chiave primaria  PluralizingTableNameConvention  Nome tabella = plurale nome classe  PluralizingEntitySetNameConvention  Proprietà di navigazione = plurale nome tipo dato  DecimalPropertyConvention  Setta la precision e la scale (18,2) per le proprietà decimali  Tanti tanti altri...  Vedi: System.Data.Entity.ModelConfiguration.Conventions
  • 22. #sqlsatParma #sqlsat355November 22nd, 2014 Operazioni basilari (CRUD) using (var db = new BloggingContext()) { var blog = new Blog(); blog.Name = "Il mio primo blog"; db.Blogs.Add(blog); db.SaveChanges(); } using (var db = new BloggingContext()) { var blog = db.Blogs.Find(1); Console.WriteLine(blog.Name); } using (var db = new BloggingContext()) { var blog = db.Blogs.Find(1); blog.Name = "Nome modificato!!"; db.SaveChanges(); } using (var db = new BloggingContext()) { var blog = db.Blogs.Find(1); db.Blogs.Remove(blog); db.SaveChanges(); }
  • 23. #sqlsatParma #sqlsat355November 22nd, 2014 DEMO 02  Create  Read  Update  Delete
  • 24. #sqlsatParma #sqlsat355November 22nd, 2014  LINQ Query  Lambda Expression Query LINQ: Filtrare using (var db = new BloggingContext()) { var results = from post in db.Posts where post.Title.Contains("trovami") select post; foreach (var result in results) Console.WriteLine(result.Title); } using (var db = new BloggingContext()) { var results = db.Posts .Where(post => post.Title.Contains("trovami")); foreach (var result in results) Console.WriteLine(result.Title); } SELECT [Extent1].[PostId] AS [PostId], [Extent1].[Title] AS [Title], [Extent1].[Content] AS [Content], [Extent1].[BlogId] AS [BlogId] FROM [dbo].[Posts] AS [Extent1] WHERE [Extent1].[Title] LIKE N'%trovami%'
  • 25. #sqlsatParma #sqlsat355November 22nd, 2014 Query LINQ: Ordinare using (var db = new BloggingContext()) { var results = from post in db.Posts orderby post.Title select post; foreach (var result in results) Console.WriteLine(result.Title); } using (var db = new BloggingContext()) { var results = db.Posts .OrderBy(t => t.Title); foreach (var result in results) Console.WriteLine(result.Title); } using (var db = new BloggingContext()) { var results = db.Posts .Where(t => t.Content.Contains("trovami")) .OrderBy(t => t.Title); foreach (var result in results) Console.WriteLine(result.Title); } SELECT [Extent1].[PostId] AS [PostId], [Extent1].[Title] AS [Title], [Extent1].[Content] AS [Content], [Extent1].[BlogId] AS [BlogId] FROM [dbo].[Posts] AS [Extent1] ORDER BY [Extent1].[Title] ASC SELECT [Extent1].[PostId] AS [PostId], [Extent1].[Title] AS [Title], [Extent1].[Content] AS [Content], [Extent1].[BlogId] AS [BlogId] FROM [dbo].[Posts] AS [Extent1] WHERE [Extent1].[Content] LIKE N'%trovami%' ORDER BY [Extent1].[Title] ASC LINQ Query OrderBy Lambda Expression OrderBy Lambda Expression Where + OrderBy
  • 26. #sqlsatParma #sqlsat355November 22nd, 2014 Query LINQ: Join(are)  Esplicite  Implicite using (var db = new BloggingContext()) { var results = from blog in db.Blogs join post in db.Posts on blog.BlogId equals post.BlogId select new { BlogName = blog.Name, PostTitle = post.Title }; foreach (var result in results) Console.WriteLine("blog: {0} post: {1}", result.BlogName, result.PostTitle); } using (var db = new BloggingContext()) { var results = from blog in db.Blogs from post in blog.Posts select new { BlogName = blog.Name, PostTitle = post.Title }; foreach (var result in results) Console.WriteLine("blog: {0} post: {1}", result.BlogName, result.PostTitle); } SELECT [Extent1].[BlogId] AS [BlogId], [Extent1].[Name] AS [Name], [Extent2].[Title] AS [Title] FROM [dbo].[Blogs] AS [Extent1] INNER JOIN [dbo].[Posts] AS [Extent2] ON [Extent1].[BlogId] = [Extent2].[BlogId]
  • 27. #sqlsatParma #sqlsat355November 22nd, 2014 Query: Stored Procedure  Raw SQL Queries http://msdn.microsoft.com/data/jj592907 public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } public Blog GetBlogById(int blogId) { return this.Blogs .SqlQuery("proc_GetBlogById @p0", blogId) .FirstOrDefault(); } public Blog GetBlogById2(int blogId) { return this.Blogs .SqlQuery("proc_GetBlogById @BlogId", new SqlParameter("@BlogId", blogId)) .FirstOrDefault(); } }
  • 28. #sqlsatParma #sqlsat355November 22nd, 2014 Strumenti di Logging  Proprietà Log a delegato  Personalizzazione del contenuto e del formato  Sistema di intercettazione a moduli per maggiore controllo e flessibilità  Gli Interceptor possono essere aggiunti da config senza dovere ricompilare il progetto BloggingContext.Database.Log = Console.Write;
  • 29. #sqlsatParma #sqlsat355November 22nd, 2014 DEMO 03  Filtrare  Ordinare  Join(are)  Stored Procedure  Logging
  • 30. #sqlsatParma #sqlsat355November 22nd, 2014 Evolvere il database: Code-First Migrations  Sempre sotto il nostro controllo  Personalizzazioni (anche estreme) da codice  Semplicità di utilizzo  Enable-Migrations  Add-Migration  Update-Database  http://msdn.microsoft.com/data/jj591621
  • 31. #sqlsatParma #sqlsat355November 22nd, 2014 DEMO 04  Enable-Migrations  Add-Migrations  Update-Database  Migrate.exe packagesEntityFramework.6.1.1tools
  • 32. #sqlsatParma #sqlsat355November 22nd, 2014 Importare un database esistente  Da zero senza reverse engineering  Con strumenti di automazione:  Entity Framework Tools (incluso in VS2013) www.microsoft.com/download/details.aspx?id=40762  Entity Framework Power Tools (NuGet)  EntityFramework Reverse POCO Generator (NuGet)  Code First Migrations anche per database esistenti/importati http://msdn.microsoft.com/data/dn579398
  • 33. #sqlsatParma #sqlsat355November 22nd, 2014 DEMO 05  Importare un database esistente  Entity Framework Tools  Entity Framework Power Tools  EntityFramework Reverse POCO Generator
  • 34. #sqlsatParma #sqlsat355November 22nd, 2014 Strumenti di Profiling  SQL Server Profiler (Microsoft)  SQL XEvent Profiler (free Idera)  EF Profiler (commerciale, installer HibernatingRhinos.com)  ORM Profiler (commerciale, NuGet)  LINQ Insight Express (free, NuGet)
  • 35. #sqlsatParma #sqlsat355November 22nd, 2014 What’s Next  Versione 7 più leggera ed estensibile  Sarà disponibile per nuove piattaforme (Store App, Phone App, Net Core, Mac, Linux)  Abiliterà nuovi data store, come sistemi non- relazionali  Il passaggio a EF7 sarà semplificato il più possibile (DbContext e DbSet saranno presenti)  Sviluppato e disponibile in pre-alpha su GitHub https://github.com/aspnet/EntityFramework
  • 36. #sqlsatParma #sqlsat355November 22nd, 2014 In pillole…  Include(t => t.Posts) evita il lazy loading http://msdn.microsoft.com/data/jj574232  Find(1) ricerca per chiave http://msdn.microsoft.com/data/jj573936  Entity<>.ToTable("Posts", "Blog") per creare Posts nello schema Blog  Async Query & Save http://msdn.microsoft.com/data/jj819165  CRUD con Stored Procedure http://msdn.microsoft.com/data/dn468673
  • 37. #sqlsatParma #sqlsat355November 22nd, 2014 In pillole…  Considerazioni sulle performance http://msdn.microsoft.com/data/hh949853  Migliorare lo startup dell’applicazione con NGen http://msdn.microsoft.com/data/dn582034  Pre-generare le Mapping Views (EDM) per velocizzare dell’inizializzazione del DbContext http://msdn.microsoft.com/data/dn469601  Gestione della concorrenza ottimistica http://msdn.microsoft.com/data/jj592904
  • 38. #sqlsatParma #sqlsat355November 22nd, 2014 In pillole…  Migrate.exe per controllare e automatizzare il processo di migrazione del database http://msdn.microsoft.com/data/jj618307  AsNoTracking() http://msdn.microsoft.com/data/jj556203  Disabilitare il tracking automatico http://msdn.microsoft.com/data/jj556205  Unit Test: testare isolandosi dal database http://msdn.microsoft.com/data/dn314429

Editor's Notes

  1. ORM = Object-relational mapping RDBMD = Relational database management system
  2. Caching (first-level), non è la cache classica, la query viene fatta lo stesso (con eccezione del metodo Find), ma almeno l’oggetto non viene ricreato in memoria. Esiste anche una cache di secondo livello per caching query results
  3. The architecture of the ADO.NET Entity Framework, from the bottom up, consists of the following: Data source specific providers, which abstract the ADO.NET interfaces to connect to the database when programming against the conceptual schema. Map provider, a database-specific provider that translates the Entity SQL command tree into a query in the native SQL flavor of the database. It includes the Store-specific bridge, which is the component responsible for translating the generic command tree into a store-specific command tree. EDM parser and view mapping, which takes the SDL specification of the data model and how it maps onto the underlying relational model and enables programming against the conceptual model. From the relational schema, it creates views of the data corresponding to the conceptual model. It aggregates information from multiple tables in order to aggregate them into an entity, and splits an update to an entity into multiple updates to whichever table(s) contributed to that entity. Query and update pipeline, processes queries, filters and updates requests to convert them into canonical command trees which are then converted into store-specific queries by the map provider. Metadata services, which handle all metadata related to entities, relationships and mappings. Transactions, to integrate with transactional capabilities of the underlying store. If the underlying store does not support transactions, support for it needs to be implemented at this layer. Conceptual layer API, the runtime that exposes the programming model for coding against the conceptual schema. It follows the ADO.NET pattern of using Connection objects to refer to the map provider, using Command objects to send the query, and returning EntityResultSets or EntitySets containing the result. Disconnected components, which locally cache datasets and entity sets for using the ADO.NET Entity Framework in an occasionally connected environment. Embedded database: ADO.NET Entity Framework includes a lightweight embedded database for client-side caching and querying of relational data. Design tools, such as Mapping Designer, are also included with ADO.NET Entity Framework, which simplifies the job of mapping a conceptual schema to the relational schema and specifying which properties of an entity type correspond to which table in the database. Programming layer, which exposes the EDM as programming constructs which can be consumed by programming languages. Object services, automatically generate code for CLR classes that expose the same properties as an entity, thus enabling instantiation of entities as .NET objects. Web services, which expose entities as web services. High-level services, such as reporting services which work on entities rather than relational data.
  4. POCO = Plain Old CLR Object
  5. ICollection, per il semplice fatto che l’indexer [n] (random access) non è mappato da EF, e finirebbe per querare il database ogni volta che lo si usa.
  6. Se sql express è presente e attivo, NuGet configurerà in config il connection factory sql express, altrimenti localdb.
  7. Personalizzazione contenuto e formato ereditanto DatabaseLogFormatter Interceptor per logging su file: System.Data.Entity.Infrastructure.Interception.DatabaseLogger
  8. Profiler extended events, free tool: Xevent di idera.com