Distributed Information Systems: Lecture 4 - Entity Framework Based On Julia Lerman, Chs 1-8
Distributed Information Systems: Lecture 4 - Entity Framework Based On Julia Lerman, Chs 1-8
Distributed Information Systems: Lecture 4 - Entity Framework Based On Julia Lerman, Chs 1-8
Distributed
Information
Systems
L E C T U R E 4 – E N T I T Y F RA M E W O R K
BASED ON JULIA LERMAN,CHS 1-8
Agenda
• Background – domain models
• Code first components
• Entity framework example
• Loose ends
◦ Constraints - Fluent API and Data Annotations
◦ Connection strings - Database selection
◦ Migrations
◦ LINQ
2
Object-relational
mapping
• Entity Framework
◦ Minimize SQL
◦ All SQL relations can be represented
◦ 0 to many
◦ 1 to many
◦ Many to many
◦ Type-checking
◦ Fewer run-time errors
◦ Downloaded from NuGet repository
3
Code first
• Developers create classes
◦ E.g. Person, Course etc
◦ Hence the name code first
◦ As opposed to database first
• Entity Framework builds and updates the SQL database with this
information
4
Code first components
1. POCO classes
◦ Plain old C# objects
◦ Name analogous to POJO
◦ Plain old java objects
◦ Or, POTS
◦ Plain old Telephone system
◦ No signs of Entity Framework
◦ But EF can draw many inferences from structure
◦ Convention over configuration
2. Entity Configuration
◦ Data annotations or Fluent
3. Application
◦ Uses POCO classes and entity configuration
5
Resources
• Based on personal experience
◦ Julia Lerman and Rowan Miller, “Programming Entity Framework: Code
first,” O’Reilly Media, ISBN 978-1449312947
◦ Fast-paced, technically-sound introduction
◦ Should know cover-to-cover
◦ Stack Overflow
◦ Professionally courteous responses to advanced questions
◦ Links to relevant resources – blogs etc
6
Technology evolution
EF Core 2
Aug 14, 2017
EF Core 1
Cross-platform, NoSQL, Jun ’16
EF 6 Used in
EdvisionApp
Last Windows exclusive release, Oct 2013
System.Data.Entity.DbContext
(introduced in EF 4.1, ~2011)
System.Data.Entity.Infrastructure.ObjectContext
(Introduced with .NET 3.5, ~2007)
Dot net class library
Initial release ~ 2002
7
Using Entity Framework
• We will learn by example, starting with a simple MVC application
• For those interested
◦ Highly recommended: Simple Vet office, as in Julia Lerman book
• Learnings
◦ Installing Entity Framework
◦ Architecting use of Entity Framework
◦ Connecting to the database
◦ Configuring entities
8
Entity framework use
• Consider the typical application
Companies Users
Quotes Products
Employees Vendors
9
Typical data
architecture
Entities
Application { Used in
… <business logic>
Class Comapny(Id, Name, …)
CreateCompany(Id = 1) Class Quote(Id, Date, …)
ReadQuote(Id = 1)
Related Class Employee(Id, Name, …)
… <business logic> DBContext : EntityFramework …
} in
Company(Id, Name, …)
Quote(Id, Date, …)
Employee(Id, Name, …)
…
Persisted
in Companies
Quotes
Employees
10
Example setup
• We will map our solution to the data architecture
11
Example setup
Application { Used in Entities
… <business logic>
CreateCompany(Id = 1) ApplicationDbContext Class Company(Id, Name, …)
Class Quote(Id, Date, …)
ReadQuote(Id = 1) Related
… <business logic> ApplicationDbContext : DbContext …
in
}
DbSet<Company> Companies EF_Models
HomeController DbSet<Equity> Equities
…
Persisted
in Companies
Equities
12
Github project
• https://github.com/magrawal-USF/MVC_EF_Start.git
13
Additions to MVC start
project
• Company and Equity POCO classes added to Models
• ApplicationDbContext.cs added to the project
◦ Conventionally, in a folder named DataAccess
14
EntityFramework in
action
• We have done a lot of little things, what should happen if all goes
well?
◦ The database should be created on local SQL Server instance
• Run solution
• Open SQL Server Object Explorer
◦ Personal opinion: One factor behind Microsoft’s success among developers
is their attention to detail in making things work easily
◦ Welcoming to newbies
15
Results
• Out of the box, EF
worked with the default
database setup in Visual
Studio
• Use View → SQL Server
Object Explorer to find it
◦ May need to use “Add
SQL Server” to locate the
database
• EF created and
populated the database
as specified in our
connection string
16
Basic EntityFramework
conventions
• View the tables in SQL Server object explorer
◦ Id column automatically identified as the
primary key
◦ AnimalType_Id added as a foreign key
◦ How did these happen?
◦ EntityFramework code first
• Remember?
◦ Convention over configuration
17
EntityFramework
conventions
• Class names are singular
◦ E.g. Person
18
EF conventions (contd.)
• Primary key
◦ Id field, or
◦ <class name>Id field
19
Recommended model
for foreign key
relationship
• See example 4-2 in JL
• Preferred model for Visit class would be:
public class Visit
{
public int Id { get; set; }
public DateTime Date { get; set; }
public String ReasonForVisit { get; set; }
public String Outcome { get; set; }
public Decimal Weight { get; set; }
//public int PatientId { get; set; }
20
Progress so far
• Questions?
21
Loose ends in database
use
• Specifying constraints
◦ How do you specify more complex constraints?
◦ E.g. required fields
• Connection strings
◦ How do you connect to databases in non-default locations?
◦ E.g. AWS, Azure
• Migrations
◦ How do you gracefully change the data model?
◦ Not just deleting the existing database
• LINQ
◦ How do you work with the data in the application?
◦ Without using SQL
22
Specifying constraints
• 2 methods
◦ Data annotations in model classes, e.g.
[Required]
public string Name { get; set; }
◦ Fluent API
• Personal preference
◦ Fluent API
◦ Keeps class definitions clean and readable
◦ More comprehensive than data annotations
◦ Allows complex relationships between classes
23
Organizing Fluent
configurations
• Convenient to organize fluent configurations for each class in a separate configuration class
◦ EntityTypeConfiguration
◦ But, not yet supported in EntityFrameworkCore (Jan 2018)
◦ http://www.learnentityframeworkcore.com/configuration/fluent-api
24
Organizing Fluent –
contd.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AnimalTypeConfiguration());
}
• In VetContext.cs, add the OnModelCreating() method
◦ Invoked when the database is created
◦ Invoke the fluent configuration
◦ adapted from JL listing 1-6
25
Fluent configuration
observations
• Model classes have no reference to the constraints
◦ Look as clean as before
◦ Data Annotations can clutter up the class definitions
◦ And mix database constraints into POCO classes
26
Some fluent methods
• HasKey(t => t.Identifier);
◦ Specifies that Identifier column is the primary key
27
Developer convenience
feature - Initialization
• During development, it is convenient for code first to modify database
schema, instead of deleting the database
static void Main(string[] args)
{
System.Data.Entity.Database.SetInitializer(
new
System.Data.Entity.DropCreateDatabaseIfModelChanges<VetContext>());
// OR
//System.Data.Entity.Database.SetInitializer(
// new
System.Data.Entity.CreateDatabaseIfNotExists<VetContext>());
…;
}
28
Code first
recommendations
• Fluent API over data annotations
◦ Data annotations are convenient
◦ But Fluent is more comprehensive
◦ Also much cleaner to read
◦ Subjective opinion
29
Database location and
authentication
• We have connected to the default local SQL Server database
◦ How do you connect to a specified database on a specific server
◦ How do you provide authentication information?
30
Connection strings
• The connection parameters to the database are specified in the
application as the connection string
◦ Database location (IP address)
◦ Database instance
◦ Username, password
• Specified in appsettings.json
31
Connection string –
contd.
"Data": {
"IEXTrading": {
"ConnectionString":
"Server=(localdb)\\MSSQLLocalDB;Database=IEXTrading;Trusted_Connection=True;M
ultipleActiveResultSets=true"
}
}
• In Startup.cs:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:IEXTrading:Connec
tionString"]));
32
Migrations
• Local database
◦ You can delete and create database at will
• Deployment servers
◦ Permissions are limited after initial database creation
◦ How do you
◦ add a column
◦ delete column
◦ change column type etc?
33
Migrations – contd.
• Migrations references
◦ http://ilmatte.wordpress.com/2012/12/05/entity-framework-5-code-first-enabling
-migrations-in-a-real-project
/
◦ http://
stackoverflow.com/questions/13469881/how-do-i-enable-ef-migrations-for-multi
ple-contexts-to-separate-databases
• Links refer to older version of Entity Framework
◦ But describe concepts nicely
◦ Verified Jan 2018
• Important (https://stackoverflow.com/a/22262341/1385857 )
◦ Using a corporate shared drive may prevent proper execution of EF install scripts
◦ At USF, saving on U: breaks migration commands, even though regular EF functionality is fine
34
Migrations – contd.
• Two scenarios
◦ Setup database from scratch using migrations
◦ Migrations creates the tables
◦ Use SQL etc. to populate tables
◦ We are following this scenario
35
Migrating from scratch
• 2 steps
◦ Enable migrations
◦ Done one-time at the start of the project
◦ PM> Enable-Migrations
◦ Creates baseline model from existing database structure
◦ Add migration
◦ Done each time a model change is to be applied to the database
◦ PM> Add-Migration <Migration name>
◦ Detects the difference between current code-first model and all prior migrations
◦ Adds needed operations to pending migrations
◦ PM> Update-Database –verbose
◦ Applies these updates to the database
36
Enabling Migrations
• EF 6 known to be compatible with .NET framework
◦ But not with .NET Standard
37
Effects of enabling
Migrations
• Migrations folder
added to the project
with the DbContext
◦ Check out the
contents of the Up()
method
◦ These statements are applied
to the database
38
Changing model
• To change model with migrations
◦ E.g.
◦ Add LastName property to Patient class
39
Naming migrations
• Personal experience
◦ Migrations accumulate
◦ Sorted by name
◦ But useful to have them sorted in creation order
• More details
◦ Check out first blog link in migrations references (slide) for examples
◦ Also MSDN documentation etc.
40
Exercise
• Update the VetOffice object model by adding 2 relevant and related
classes
• Use migrations to update the database with the updated object model
41
Migrations – with
existing databases
• http://
stackoverflow.com/questions/9270887/how-do-i-create-a-migration-for
-an-existing-database-in-entityframework-4-3
• http://
www.goodreads.com/author_blog_posts/2101481-using-ef-4-3-code-firs
t-migrations-with-an-existing-database
• http://
www.ralphlavelle.net/2012/02/using-entity-framework-code-first-with
_26.html
42
LINQ
• Querying entities typically done using Linq
◦ Language integrated Query
◦ Filter items in database using SQL-like syntax
◦ But strongly typed, e.g.
// https://stackoverflow.com/questions/11597373/the-specified-type-member-date-is-not-supported-in-linq-to-entities-exception
43
LINQ usage - where
• Common constructs used in Linq queries
• Where
◦ Apply constraints
◦ E.g.
44
LINQ usage - select
• Select
◦ Retrieve collections
◦ E.g. add a list of comments to each visit
public class Visit
{
public …
public List<VisitComment> Comments { get; set; }
}
45
LINQ usage - select
• Select
◦ Retrieve collections
◦ How to retrieve all the comments for the visits by a patient?
46
LINQ usage - Include
• Entity framework tries to optimize queries
◦ Related objects are not loaded unless specifically asked for
• Include asks for related entities to also be retrieved from the database
• Consider 2 queries
◦ What is the difference in outputs?
◦ Why?
47
Next steps
• Complete the Sports Store example in the book
48
Relationship fix-up
• Queries typically allow selection at top level
◦ Select Patients meeting certain criteria
◦ E.g. Patients over age 50
• What if
◦ you also wanted to filter visits by certain criteria?
◦ E.g. Visits by these patients this year
• http://
blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditio
nal-include.aspx
49
Modeling inheritance
• How do you model inheritance in general?
◦ Table per hierarchy
◦ 1 common table for all classes in the hierarchy
◦ Common columns for base class fields
◦ Discriminator column
◦ Value in row == <class name>
◦ Plus, columns for inherited classes
◦ Table per type
◦ 1 table for base class
◦ Separate tables for specialized classes
◦ With foreign key to base class
50
Modeling inheritance –
contd.
• Table per concrete type
◦ One table per class
◦ Includes all fields from all inherited classes
• Personal preference
◦ Fewer joins
◦ Easier to understand
◦ But more redundancy
◦ Change in base class requires change in all concrete classes
51
Modeling inheritance –
contd.
• http://
weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strate
gies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarch
y-tph.aspx
◦ Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)
52
Domain models
• Layered architectures are common in modern applications
• In model-driven design, the domain layer represents the model of the
business
◦ Rest of the architecture (services etc) solve complex technical problems
Application layer
Infrastructure layer
(e.g. EF dbContext)
53
Entity vs Value objects
• Source: Eric Evans, Domain-driven design
• Consider
◦ Object: User, customer
◦ Object: State, Country etc
54
Modeling entities and value
objects
• Entities
◦ Keep class definition simple
◦ Find ways of distinguishing objects
◦ Define what it means to be the same thing
• Value objects
◦ Focus on expressing the meanings of its attributes
◦ Generally, treat value objects as immutable
◦ i.e. cannot be changed by the application
◦ Whole value pattern
◦ Gather all relevant attributes into the value object, and make the value object a property of an entity
◦ E.g. put street, city, postal code etc in an address object and make the address object a property of
the customer entity
◦ Simplifies entities
55
Services
• Models are classes
◦ But many concepts in a domain are not things
◦ They are activities (verbs, not nouns)
◦ And are not a natural responsibility for an entity or value object in the
model
◦ E.g. SendEmailService, TransferFundsService
56
Other important model
concepts
• Explicit constraints
◦ Require data that is not a part of the object’s definition
◦ Consolidates rules into one object
◦ Simplifies focus on constraints by isolating them into constraint objects
• Specifications
◦ Value objects that test whether an object satisfies some criteria
◦ E.g. IsDelinquent()
◦ Useful for validation, selection and building to order
• Processes
◦ Can be modeled as services
◦ Make important algorithms into objects
57
Summary
• Background
• Code first components
• Entity framework example
• Loose ends
◦ Constraints - Fluent API and Data Annotations
◦ Connection strings - Database selection
◦ Migrations
◦ LINQ
58