DbContext
class. The API can also be used in the development process with Database First and Model First approaches. For more information, see When is Code First not code first? In the blog of the development team Entity Framework.EntityObject
inheritance.h
1
and the links in the menu, as in the example: <!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> </head> <body> <div class="page"> <div id="header"> <div id="title"> <h1>Contoso University</h1> </div> <div id="logindisplay"> @Html.Partial("_LogOnPartial") </div> <div id="menucontainer"> <ul id="menu"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Students", "Index", "Student")</li> <li>@Html.ActionLink("Courses", "Index", "Course")</li> <li>@Html.ActionLink("Instructors", "Index", "Instructor")</li> <li>@Html.ActionLink("Departments", "Index", "Department")</li> </ul> </div> </div> <div id="main"> @RenderBody() </div> <div id="footer"> </div> </div> </body> </html>
h2
tag.#main
block add clear: both
: #main { clear: both; padding: 30px 30px 15px 30px; background-color: #fff; border-radius: 4px 0 0 0; -webkit-border-radius: 4px 0 0 0; -moz-border-radius: 4px 0 0 0; }
nav
and #menucontainer
clear: both; float: left
clear: both; float: left
: nav, #menucontainer { margin-top: 40px; clear: both; float: left; }
Student
and Enrollment
, and a one-to-many relationship between Course
and Enrollment
. In other words, a student can attend any number of courses, and a course can have any number of students attending it. using System; using System.Collections.Generic; namespace ContosoUniversity.Models { public class Student { public int StudentID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } }
StudentID
property will be the primary key of the corresponding table. By default, the Entity Framework treats a property with an ID
or classname ID
as the primary key.Enrollments
- navigation property . Navigation properties contain other entities related to the current. In this case, the Enrollments
property contains all the Enrollment
entities associated with the current Student
entity. In other words, if a certain Student
record in a database has a link to two Enrollment
records (records containing primary key values for a student in the StudentID
foreign key StudentID
), the property of this Enrollments
record will contain two Enrollment
entities.virtual
modifier to use the Entity Framework feature called lazy loading . (the essence of Lazy loading will be explained later, in Reading Related Data ) If the navigation property can contain several entities (in many-to-many and one-to-many relationships), its type should be an ICollection
. using System; using System.Collections.Generic; namespace ContosoUniversity.Models { public class Enrollment { public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public decimal? Grade { get; set; } public virtual Course Course { get; set; } public virtual Student Student { get; set; } } }
decimal
type indicates that the Grade
property is nullable. A rating set to null is different from a zero rating — null means that the rating has not been set yet, whereas 0 is already a value.StudentID
property is a foreign key, and the corresponding navigation property Student
. The entity Enrollment
associated with one Student
entity, therefore a property can contain only one entity of the specified type (as opposed to Student
.
Enrollments
).CourseID
property is a foreign key, and the corresponding navigation property Course
. Entity Enrollment
associated with one Entity Course
. using System; using System.Collections.Generic; namespace ContosoUniversity.Models { public class Course { public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } }
Enrollments
- navigation property. Entity Course
can be associated with an infinite set of Enrollment
entities.System
.
Data
.
Entity
.
DbContext
. In the code, you define which entities to include in the data model, and you can also define the behavior of the Entity Framework itself. In our code, this class is called SchoolContext
. using System; using System.Collections.Generic; using System.Data.Entity; using ContosoUniversity.Models; using System.Data.Entity.ModelConfiguration.Conventions; namespace ContosoUniversity.Models { public class SchoolContext : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Enrollment> Enrollments { get; set; } public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } }
DbSet
property for each set of entities. In Entity Framework terminology, an entity set refers to a database table, and an entity refers to an entry in a table.
OnModelCreating
protects table names from pluralization, and if you don’t do this, you get table names such as Students
, Courses
, Enrollments
. Otherwise, the table names will be Student
, Course
, Enrollment
. Developers are arguing about whether the names of tables should be pluralized or not. We use a single form, but the important thing is that you can choose whether to include this line in the code or not. <add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
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(); 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(); } } }
Seed
method accepts a database context object as an input parameter and uses it to add new entities to the database. For each type of entity, the code creates a collection of new entities, adding them to the corresponding DbSet property, and then saves the changes to the database. There is no need to call SaveChanges
after each group of entities, as we have done, but it helps to identify the problem in case of exceptions.using
: using System.Data.Entity; using ContosoUniversity.Models; using ContosoUniversity.DAL;
Application_Start
method, call the Entity Framework method, which runs the base initialization code: Database.SetInitializer<SchoolContext>(new SchoolInitializer());
SchoolContext
class), and if it is out of sync, the application deletes and re-creates the base.Student
controller, click on the Controllers folder in Solution Explorer , click Add , Controller . In Add Controller, perform the following actions and changes and click Add :private SchoolContext db = new SchoolContext ();
Index
action collects a list of students from the Students
property from an instance of the database context: public ViewResult Index() { return View(db.Students.ToList()); }
Student
set. To customize the headings and column sequence, open Views \ Student \ Index. cshtml and replace the code with: @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> @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>
EdmMetadata
used to define the Entity Framework when model and base have become unsynchronized.SchoolInitializer
class.ID
or classname ID
recognized as primary keys.SchoolContext
).Source: https://habr.com/ru/post/133316/
All Articles