Slide 1

Slide 1 text

Richie Rump @Jorriss www.jorriss.net

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

• Object-Relational Mapping Framework • Allows developers to retrieve database data from an object model. • Converts object data into relational data • Uses your classes • Generates SQL

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

• Code First Model • DbContext • Fluent API

Slide 6

Slide 6 text

• Bug Fixes • Semantic Versioning Because Entity Framework 4.2 is better than: Entity Framework 4.1 Service Pack 2 Update 1 Refresh Release To Web Pack

Slide 7

Slide 7 text

• Code First Migrations • Data Annotations on non-public properties • Additional configuration file settings • Removal of EdmMetadata table • Bug Fixes

Slide 8

Slide 8 text

• Added –IgnoreChanges to enable CodeFirst against existing database. • More inevitable bug fixes.

Slide 9

Slide 9 text

Entity Framework 4.0 included with .Net 4.0 EF 4.1 - Code First & DbContext EF 4.2 – Bug Fixes EF 4.3 - Migrations EF 4.3.1 – Bug Fixes

Slide 10

Slide 10 text

Design First Code First New Database Existing Database Model First Create .edmx model in designer Generate DB from .edmx Classes auto-generate from .edmx Database First Reverse engineer .edmx model Classes auto-generate from .edmx Code First Define classes & mapping in code Database auto-created at runtime Code First Define classes & mapping in code Adapted from Programming Entity Framework: Code First by Julie Learman and Rowan Miller page 3.

Slide 11

Slide 11 text

Lovingly stolen from Programming Entity Framework: Code First by Julie Lerman and Rowan Miller page 9.

Slide 12

Slide 12 text

In a word: Nuget

Slide 13

Slide 13 text

• What does code look like? • How do we use it? • Stop! Demo time.

Slide 14

Slide 14 text

• Convention over configuration – Database naming – Primary Key • SQL Server Express – default database • dbContext Class

Slide 15

Slide 15 text

• It’s not enough to use convention. • Tells EF how to map the object model to the database model. • Place annotations directly against the property in your class. • System.ComponentModel.DataAnnotations

Slide 16

Slide 16 text

• Key – Defines a Primary Key • Column – Defines DB column name • Table – Defines table name for a class • Required – Defines a Required DB field • NotMapped – Property not in DB mapping • MinLength() – Min length for a property • MaximumLength() – Max length for property • Range() – Defines a valid value range

Slide 17

Slide 17 text

[Table("Product_Order")] public class Order { [Key] [Column("Order_ID")] public int OrderId { get; set; } public DateTime Date { get; set; } public OrderState State { get; set; } public string Item { get; set; } [Range(1, 25)] public int Quantity { get; set; } [MinLength(3, ErrorMessage="What are you thinking?")] [MaxLength(50, ErrorMessage="ERROR!! FAILZ!!!!")] public string Name { get; set; } [NotMapped] public string Note { get; set; } }

Slide 18

Slide 18 text

• Allows you to configure EF without polluting your classes with annotations. • Cleaner code • Can have all EF mapping code in one file. • Some things you can only do in the Fluent API

Slide 19

Slide 19 text

Data Annotations Fluent API modelBuilder.Entity() .Property(p => p.Id).HasColumnName("Order_ID"); [Column("Order_ID")] public int Id { get; set; }

Slide 20

Slide 20 text

• This time it will work…I swear!

Slide 21

Slide 21 text

• Expressed via the navigation properties in your classes public class Order { public int Id { get; set; } public OrderState State { get; set; } }

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

public OrderConfiguration() { ToTable("Order"); Property(p => p.OrderStateID).HasColumnName("Order_State_ID"); HasRequired(p => p.State).WithMany() .HasForeignKey(f => f.OrderStateID); } public class Order { public int Id { get; set; } public int OrderStateID { get; set; } public OrderState State { get; set; } } public class OrderState { public int Id { get; set; } }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

public OrderConfiguration() { ToTable("Order"); Property(p => p.PersonID).HasColumnName("Person_ID"); HasRequired(p => p.SalesPerson).WithMany(t => t.Orders) .HasForeignKey(f => f.PersonID); } public class Person { public int PersonID { get; set; } ... public List Orders { get; set; } } public class Order { public int Id { get; set; } public int PersonID { get; set; } public virtual Person SalesPerson { get; set; } }

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

HasMany(p => p.Items).WithMany(t => t.Orders) .Map(m => { m.ToTable("Order_Item"); m.MapLeftKey("Order_ID"); m.MapRightKey("Item_ID"); }); public class Item { public int Id { get; set; } ... public List Orders { get; set; } } public class Order { public int Id { get; set; } ... public List Items { get; set; } }

Slide 28

Slide 28 text

• New way to interact with EF objects • Makes interaction with EF MUCH simpler • Encapsulates existing EF objects such as ObjectContext, ObjectSet and ObjectQuery

Slide 29

Slide 29 text

• DbContext – Provides facilities querying and persisting object changes. • DbSet – Represents an entity for CRUD operations • Change Tracker API and Validation API are other features

Slide 30

Slide 30 text

EFTestContext context = new EFTestContext(); var query = context.Orders .Where(c => c.PersonID == 22) .Include(c => c.State) .ToList(); List orders = context.Orders.ToList();

Slide 31

Slide 31 text

• Easy way to retrieve an object via the Primary Key. EFTestContext context = new EFTestContext(); Person p = context.People.Find(1);

Slide 32

Slide 32 text

EFTestContext context = new EFTestContext(); Person p = new Person { FirstName = "Testy", LastName = "User" }; context.People.Add(p); context.SaveChanges();

Slide 33

Slide 33 text

EFTestContext context = new EFTestContext(); Person p = context.People.Find(1); context.People.Remove(p); context.SaveChanges();

Slide 34

Slide 34 text

• Migrations is a new way to generate database changes automatically • It’s controlled through PowerShell commands • Can migrate forward or rollback DB changes. • Migrations can be handled automatically. EF will automatically apply change scripts

Slide 35

Slide 35 text

• To turn Migrations on • This creates a Migration folder in project • Creates Configuration.cs file • Creates __MigrationHistory system table in DB PM> Enable-Migrations

Slide 36

Slide 36 text

• “Initial” is the name of the migration • The –IgnoreChanges switch tells EF to create an empty migration. Use this if this is the first migration and EF did not create the DB. • Creates a cs file with the changes since the last migration. • Does not apply changes to DB. PM> Add-Migration "Inital" -IgnoreChanges

Slide 37

Slide 37 text

• Pushes the migration changes to the DB • Use the –script switch to have EF create a SQL script of the changes. • Use –TargetMigration to determine end point for DB. (Rollback/Forward) • Be careful. I wouldn’t run against a production DB. PM> Update-Database

Slide 38

Slide 38 text

Y U NO DO DATABASE RIGHT?!?!?!!!

Slide 39

Slide 39 text

• Now in Release Candidate • Install using Nuget Install-Package EntityFramework –Pre • ENUMS! • Performance Improvements • Spatial Data Types • Table-Valued Functions • Most features only in .Net 4.5

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

• Julie Lerman’s Blog http://thedatafarm.com/blog/ • Rowan Miller’s Blog http://romiller.com • Arthur Vicker’s Blog http://blog.oneunicorn.com • #efhelp on twitter • StackOverflow (of course) • PluralSight – Julie Lerman’s EF videos

Slide 42

Slide 42 text

Richie Rump @Jorriss http://jorriss.net http://slideshare.net/jorriss http://dotnetmiami.com