+ All Categories
Home > Technology > Usando el entity framework

Usando el entity framework

Date post: 25-Jun-2015
Category:
Upload: camilo-sacanamboy
View: 1,287 times
Download: 1 times
Share this document with a friend
Popular Tags:
25
USANDO EL ENTITY FRAMEWORK Juan Camilo Sacanamboy
Transcript
Page 1: Usando el entity framework

USANDO EL ENTITY FRAMEWORK

Juan Camilo Sacanamboy

Page 7: Usando el entity framework

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)

Page 13: Usando el entity framework

• 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)

Page 14: Usando el entity framework

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)

Page 15: Usando el entity framework

• 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)

Page 19: Usando el entity framework

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)

Page 20: Usando el entity framework

• @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)

Page 24: Usando el entity framework

• 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

Page 25: Usando el entity framework

Recommended