24
ENTITY FRAMEWORK CODE FIRST

Ef code first

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ef code first

ENTITY FRAMEWORK CODE FIRST

Page 2: Ef code first

• Before we begin, let's talk about convention over configuration

The usual, sir?

Page 3: Ef code first

There are three flavors of EF

Model First Database First

Code First

Page 4: Ef code first

You may have a green field project

Page 5: Ef code first

Model First • You write your business classes and tell the database to create

the database tables

Page 6: Ef code first

You may have a brown field

project

Page 7: Ef code first

Database first

You just drag the tables from Server Explorer and the classes are written for you!

Page 8: Ef code first

Code First • You write your business

classes. • DBAs design and create

tables • You map the classes to

the tables

Page 9: Ef code first

Eight easy steps to use EF Code First 1.  Create database tables 2.  Add a reference to EF 3.  Declare your database context 4.  Point to the database 5.  Write business classes 6.  Map classes to tables 7.  Map properties to columns 8.  Define relationships between tables

Page 10: Ef code first

Create database tables

Page 11: Ef code first

Add EF reference to your project

• May have to get the package from NuGet

Page 12: Ef code first

Declare a database context public class MyContext : DbContext!{! // See, it's just a class!!}!

Page 13: Ef code first

Point to the database •  In web.config <connectionStrings>!<add name="MyContext" connectionString="server=.;database=dbName;user=rcorningstone;password=pass">!</connectionStrings>!•  In the constructor public MyContext(string connectionString) : base(connectionString)!

Page 14: Ef code first

Write business classes public class Person!{! public string FirstName { get; set; }! public string lastName { get; set; }! public DateTime BirthDate { get; set; }!...!}!

Page 15: Ef code first

Map classes to tables public class MyContext : DbContext!{! public DbSet<Person> Persons { get; set; }! public DbSet<Employee> Employees { get; set;}! public DbSet<Product> Products { get; set; }! //One for each table!}!

Page 16: Ef code first

Map properties to columns public override OnModelCreating(! DbModelBuilder modelBuilder)!{! modelBuilder.Entity<OrderDetail>()! .HasKey(od => new { ! od.OrderId, od.ProductId ! });!}!

Page 17: Ef code first

Define relationships between tables public override OnModelCreating(! DbModelBuilder modelBuilder)!{! modelBuilder.Entity<Order>()! .HasMany(o => o.OrderDetails)! .WithRequired(od => od.Order);! modelBuilder.Entity<OrderDetail>()! .HasRequired(od => od.Order)! .WithMany(o => o.OrderDetails)! .HasForeignKey(od => od.OrderId)! .WillCascadeOnDelete(false);!}!

Page 18: Ef code first

Okay, so how do we read and write data?

Page 19: Ef code first

To create an entity var w = new Widget() !{! Property1 = Value1,! Property2 = Value2, ...!};!db.Widgets.Add(w);!db.SaveChanges();!

Page 20: Ef code first

To read all entities var db = new MyContext();!var allWidgets = db.Widgets;!

To read just some var db = new MyContext();!var some = from w in db.Widgets! where w.desc.contains("Panther");!

Page 21: Ef code first

To update an entity var w = db.Entities.Find(id);!w.Property1 = newValue;!db.SaveChanges();!

Page 22: Ef code first

To delete an entity db.Widgets.Remove(w);!db.SaveChanges();!

Page 23: Ef code first

What's new in EF 5 • Open Source on Codeplex • Spatial data types • Enum support • Performance!

Page 24: Ef code first

Further Study • Codeplex tutorial

•  http://codefirst.codeplex.com/

• ScottGu's blog post •  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-

development-with-entity-framework-4.aspx