Post on 25-Jun-2015
USANDO EL ENTITY FRAMEWORK
Juan Camilo Sacanamboy
ENTITY FRAMEWORK
Fuente: MSDN (Working with Data in ASP.NET MVC 3)
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
ENTITY FRAMEWORK
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
CONTOSO UNIVERSITY
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
CONTOSO UNIVERSITY
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
CONTOSO UNIVERSITY
POCO
• Plain Old CLR Objects• POCO Class Persistence ignorance• Design – Build – Test
– Independently of• Database• Persistence infrastructure code
• Focus on the business problem
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
DATA MODEL
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
STUDENT ENTITY
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
ENROLLMENT ENTITY
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
COURSE ENTITY
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
DATABASE CONTEXT
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
• SQL Server Compact<add name="SchoolContext"
connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
CONNECTION STRING
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Initializing the Database with test data
• using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Entity;using ContosoUniversity.Models;
namespace ContosoUniversity.DAL{ public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext> { protected override void Seed(SchoolContext context) { var students = new List<Student> { new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01") }, new Student { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01") }, new Student { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01") }, new Student { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01") }, new Student { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01") }, new Student { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01") }, new Student { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01") }, new Student { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") } }; students.ForEach(s => context.Students.Add(s)); context.SaveChanges();
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
• var courses = new List<Course> { new Course { Title = "Chemistry", Credits = 3, }, new Course { Title = "Microeconomics", Credits = 3, }, new Course { Title = "Macroeconomics", Credits = 3, }, new Course { Title = "Calculus", Credits = 4, }, new Course { Title = "Trigonometry", Credits = 4, }, new Course { Title = "Composition", Credits = 3, }, new Course { Title = "Literature", Credits = 4, } }; courses.ForEach(s => context.Courses.Add(s)); context.SaveChanges();
var enrollments = new List<Enrollment> { new Enrollment { StudentID = 1, CourseID = 1, Grade = 1 }, new Enrollment { StudentID = 1, CourseID = 2, Grade = 3 }, new Enrollment { StudentID = 1, CourseID = 3, Grade = 1 }, new Enrollment { StudentID = 2, CourseID = 4, Grade = 2 }, new Enrollment { StudentID = 2, CourseID = 5, Grade = 4 }, new Enrollment { StudentID = 2, CourseID = 6, Grade = 4 }, new Enrollment { StudentID = 3, CourseID = 1 }, new Enrollment { StudentID = 4, CourseID = 1, }, new Enrollment { StudentID = 4, CourseID = 2, Grade = 4 }, new Enrollment { StudentID = 5, CourseID = 3, Grade = 3 }, new Enrollment { StudentID = 6, CourseID = 4 }, new Enrollment { StudentID = 7, CourseID = 5, Grade = 2 }, }; enrollments.ForEach(s => context.Enrollments.Add(s)); context.SaveChanges(); } }}
Initializing the Database with test data
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Global.asax.cs
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Creating a Student Controller
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
StudentController.cs
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Views\Student\Index.cshtml
• @model IEnumerable<ContosoUniversity.Models.Student>
@{ ViewBag.Title = "Students";}
<h2>Students</h2>
<p> @Html.ActionLink("Create New", "Create")</p><table> <tr> <th></th> <th>Last Name</th> <th>First Name</th> <th>Enrollment Date</th> </tr>
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
• @foreach (var item in Model) { <tr> <td> @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) | @Html.ActionLink("Details", "Details", new { id=item.StudentID }) | @Html.ActionLink("Delete", "Delete", new { id=item.StudentID }) </td> <td> @Html.DisplayFor(modelItem => item.LastName) </td> <td> @Html.DisplayFor(modelItem => item.FirstMidName) </td> <td> @Html.DisplayFor(modelItem => item.EnrollmentDate) </td> </tr>}
</table>
Views\Student\Index.cshtml
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Running application
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Solution Explorer
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
Solution Explorer
Fuente: MSDN (Creating an Entity Framework Data Model for an ASP.NET MVC Application)
• Los nombres de las entidades en plural son los nombres de las tablas.
• Los nombres de las propiedades de las entidades son las columnas de las tablas.
• Las propiedades llamadas ID o *ID son reconocidas como claves primarias.
• El EF se conecta a la base de datos buscando un string de conexión que tenga el mismo nombre de la clase contexto. (SchoolContext)
Conventions