Follow author Ricardo Peres as he introduces the newest development mode for Entity Framework, Code First. With Entity Framework Code First Succinctly, you will learn the ins and outs of developing code by hand in Code First. With this knowledge, you will be able to have finer control over your output than ever before.
- Programming Entity Framework Code First Pdf Free Download Pdf
- Programming Entity Framework Code First Pdf Free Download Free
- Code First Entity Framework Mvc
Entity Framework, referred to as Entity Framework 4. The completely revised second edition of Programming Entity Framework was dedicated to teaching readers how to use this version of Entity Framework in Visual Studio 2010. When.NET 4 was released, the Entity Framework team was already hard at work on a new addition, called Code First, to. Take advantage of this course called Entity Framework tutorial for professionals to improve your Programming skills and better understand Entity Framework. This course is adapted to your level as well as all Entity Framework pdf courses to better enrich your knowledge. All you need to do is download the training document, open it and start learning Entity Framework for free. Note: If you're looking for a free download links of Code-First Development with Entity Framework Pdf, epub, docx and torrent then this site is not for you. Ebookphp.com only do ebook promotions online and we does not distribute any free download of ebook on this site.
Topics included: Setting Up • Domain Model • Database • Getting Data from the Database • Writing Data to the Database • Spatial Data Types • Handling Events • Extending Entity Framework • Exposing Data to the World • Tracing and Profiling • Performance Optimizations • Common Pitfalls.
Download Free PDF / Read Online
Publisher: Syncfusion Inc.
Published: July 2014
Format(s): PDF, Mobi(Kindle)
File size: 2.72 MB
Number of pages: 120
Download / View Link(s): PDF, Mobi
For new development, we recommend ASP.NET Core Razor Pages over ASP.NET MVC controllers and views. For a tutorial series similar to this one using Razor Pages, see Tutorial: Get started with Razor Pages in ASP.NET Core. The new tutorial:
- Is easier to follow.
- Provides more EF Core best practices.
- Uses more efficient queries.
- Is more current with the latest API.
- Covers more features.
- Is the preferred approach for new application development.
In this series of tutorials, you learn how to build an ASP.NET MVC 5 application that uses Entity Framework 6 for data access. This tutorial uses the Code First workflow. For information about how to choose between Code First, Database First, and Model First, see Create a model.
This tutorial series explains how to build the Contoso University sample application. The sample application is a simple university website. With it, you can view and update student, course, and instructor information. Here are two of the screens you create:
In this tutorial, you:
- Create an MVC web app
- Set up the site style
- Install Entity Framework 6
- Create the data model
- Create the database context
- Initialize DB with test data
- Set up EF 6 to use LocalDB
- Create controller and views
- View the database
Create an MVC web app
Open Visual Studio and create a C# web project using the ASP.NET Web Application (.NET Framework) template. Name the project ContosoUniversity and select OK.
In New ASP.NET Web Application - ContosoUniversity, select MVC.
By default, the Authentication option is set to No Authentication. For this tutorial, the web app doesn't require users to sign in. Also, it doesn't restrict access based on who's signed in.
Select OK to create the project.
Set up the site style
A few simple changes will set up the site menu, layout, and home page.
Open ViewsShared_Layout.cshtml, and make the following changes:
- Change each occurrence of 'My ASP.NET Application' and 'Application name' to 'Contoso University'.
- Add menu entries for Students, Courses, Instructors, and Departments, and delete the Contact entry.
The changes are highlighted in the following code snippet:
In ViewsHomeIndex.cshtml, replace the contents of the file with the following code to replace the text about ASP.NET and MVC with text about this application:
Press Ctrl+F5 to run the web site. You see the home page with the main menu.
Install Entity Framework 6
From the Tools menu, choose NuGet Package Manager, and then choose Package Manager Console.
In the Package Manager Console window, enter the following command:
This step is one of a few steps that this tutorial has you do manually, but that could have been done automatically by the ASP.NET MVC scaffolding feature. You're doing them manually so that you can see the steps required to use Entity Framework (EF). You'll use scaffolding later to create the MVC controller and views. An alternative is to let scaffolding automatically install the EF NuGet package, create the database context class, and create the connection string. When you're ready to do it that way, all you have to do is skip those steps and scaffold your MVC controller after you create your entity classes.
Create the data model
Next you'll create entity classes for the Contoso University application. You'll start with the following three entities:
Course <-> Enrollment <-> Student
|Course to Enrollment||One-to-many|
|Student to Enrollment||One-to-many|
There's a one-to-many relationship between
Enrollment entities, and there's a one-to-many relationship between
Enrollment entities. In other words, a student can be enrolled in any number of courses, and a course can have any number of students enrolled in it.
In the following sections, you'll create a class for each one of these entities.
If you try to compile the project before you finish creating all of these entity classes, you'll get compiler errors.
The Student entity
In the Models folder, create a class file named Student.cs by right-clicking on the folder in Solution Explorer and choosing Add > Class. Replace the template code with the following code:
ID property will become the primary key column of the database table that corresponds to this class. By default, Entity Framework interprets a property that's named
ID or classname
ID as the primary key.
Enrollments property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the
Enrollments property of a
Student entity will hold all of the
Enrollment entities that are related to that
Student entity. In other words, if a given
Student row in the database has two related
Enrollment rows (rows that contain that student's primary key value in their
StudentID foreign key column), that
Enrollments navigation property will contain those two
Navigation properties are typically defined as
virtual so that they can take advantage of certain Entity Framework functionality such as lazy loading. (Lazy loading will be explained later, in the Reading Related Data tutorial later in this series.)
If a navigation property can hold multiple entities (as in many-to-many or one-to-many relationships), its type must be a list in which entries can be added, deleted, and updated, such as
The Enrollment entity
In the Models folder, create Enrollment.cs and replace the existing code with the following code:
EnrollmentID property will be the primary key; this entity uses the classname
ID pattern instead of
ID by itself as you saw in the
Student entity. Ordinarily you would choose one pattern and use it throughout your data model. Here, the variation illustrates that you can use either pattern. In a later tutorial, you'll see how using
classname makes it easier to implement inheritance in the data model.
Grade property is an enum. The question mark after the
Grade type declaration indicates that the
Grade property is nullable. A grade that's null is different from a zero grade — null means a grade isn't known or hasn't been assigned yet.
StudentID property is a foreign key, and the corresponding navigation property is
Enrollment entity is associated with one
Student entity, so the property can only hold a single
Student entity (unlike the
Student.Enrollments navigation property you saw earlier, which can hold multiple
CourseID property is a foreign key, and the corresponding navigation property is
Enrollment entity is associated with one
Entity Framework interprets a property as a foreign key property if it's named <navigation property name><primary key property name> (for example,
StudentID for the
Student navigation property since the
Student entity's primary key is
ID). Foreign key properties can also be named the same simply <primary key property name> (for example,
CourseID since the
Course entity's primary key is
The Course entity
In the Models folder, create Course.cs, replacing the template code with the following code:
Enrollments property is a navigation property. A
Course entity can be related to any number of
We'll say more about the DatabaseGeneratedAttribute attribute in a later tutorial in this series. Basically, this attribute lets you enter the primary key for the course rather than having the database generate it.
Create the database context
The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code, you specify which entities are included in the data model. You can also customize certain Entity Framework behavior. In this project, the class is named
To create a folder in the ContosoUniversity project, right-click the project in Solution Explorer and click Add, and then click New Folder. Name the new folder DAL (for Data Access Layer). In that folder, create a new class file named SchoolContext.cs, and replace the template code with the following code:
Specify entity sets
This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.
Tomodachi life free download code. Get Tomodachi Life Download Code Generator Now! Due to intentional misuse of this service, the download code generator is now survey protected. Don't worry, it shouldn't take over 5 minutes to complete a survey. Free Tomodachi Life Download Code Generator Hello & welcome to our site. We are here to bring you free & working Tomodachi Life Download Code Generator. Our generator is the only working generator that supplies working Tomodachi Life Download Codes. We have a custom-coded algorithm that we have made to bring you working Tomodachi Life Download. Jun 27, 2014 NIntendo just emailed me this download code, don't really know what the game is about. Its the welcome version if that means anything. It would be cool if we could do an exchange or something. I'll wait for a few hours to see if anyone has anything to exchange and then to be fair i'll go on the random number generator to choose a winner. Download Tomodachi Life game for free. Tomodachi Life Free Download which is fully tested and working. We have provided this game in highly compressed form. So that everyone can download it in short time. It is working game and is tested by our team. Download links are working. Download Tomodachi Life game via direct link and full version.
You can omit the
DbSet<Course> statements and it would work the same. Entity Framework would include them implicitly because the
Student entity references the
Enrollment entity and the
Enrollment entity references the
Specify the connection string
The name of the connection string (which you'll add to the Web.config file later) is passed in to the constructor.
You could also pass in the connection string itself instead of the name of one that is stored in the Web.config file. For more information about options for specifying the database to use, see Connection strings and models.
If you don't specify a connection string or the name of one explicitly, Entity Framework assumes that the connection string name is the same as the class name. The default connection string name in this example would then be
SchoolContext, the same as what you're specifying explicitly.
Specify singular table names
modelBuilder.Conventions.Remove statement in the OnModelCreating method prevents table names from being pluralized. If you didn't do this, the generated tables in the database would be named
Enrollments. Instead, the table names will be
Enrollment. Developers disagree about whether table names should be pluralized or not. This tutorial uses the singular form, but the important point is that you can select whichever form you prefer by including or omitting this line of code.
Initialize DB with test data
Entity Framework can automatically create (or drop and re-create) a database for you when the application runs. You can specify that this should be done every time your application runs or only when the model is out of sync with the existing database. You can also write a
Seed method that Entity Framework automatically calls after creating the database in order to populate it with test data.
The default behavior is to create a database only if it doesn't exist (and throw an exception if the model has changed and the database already exists). In this section, you'll specify that the database should be dropped and re-created whenever the model changes. Dropping the database causes the loss of all your data. This is generally okay during development, because the
Seed method will run when the database is re-created and will re-create your test data. But in production you generally don't want to lose all your data every time you need to change the database schema. Later you'll see how to handle model changes by using Code First Migrations to change the database schema instead of dropping and re-creating the database.
In the DAL folder, create a new class file named SchoolInitializer.cs and replace the template code with the following code, which causes a database to be created when needed and loads test data into the new database.
Seedmethod takes the database context object as an input parameter, and the code in the method uses that object to add new entities to the database. For each entity type, the code creates a collection of new entities, adds them to the appropriate
DbSetproperty, and then saves the changes to the database. It isn't necessary to call the
SaveChangesmethod after each group of entities, as is done here, but doing that helps you locate the source of a problem if an exception occurs while the code is writing to the database.
To tell Entity Framework to use your initializer class, add an element to the
entityFrameworkelement in the application Web.config file (the one in the root project folder), as shown in the following example:
context typespecifies the fully qualified context class name and the assembly it's in, and the
databaseinitializer typespecifies the fully qualified name of the initializer class and the assembly it's in. (When you don't want EF to use the initializer, you can set an attribute on the
disableDatabaseInitialization='true'.) For more information, see Configuration File Settings.
An alternative to setting the initializer in the Web.config file is to do it in code by adding a
Database.SetInitializerstatement to the
Application_Startmethod in the Global.asax.cs file. For more information, see Understanding Database Initializers in Entity Framework Code First.
The application is now set up so that when you access the database for the first time in a given run of the application, Entity Framework compares the database to the model (your
SchoolContext and entity classes). If there's a difference, the application drops and re-creates the database.
When you deploy an application to a production web server, you must remove or disable code that drops and re-creates the database. You'll do that in a later tutorial in this series.
Set up EF 6 to use LocalDB
LocalDB is a lightweight version of the SQL Server Express database engine. It's easy to install and configure, starts on demand, and runs in user mode. LocalDB runs in a special execution mode of SQL Server Express that enables you to work with databases as .mdf files. You can put LocalDB database files in the App_Data folder of a web project if you want to be able to copy the database with the project. The user instance feature in SQL Server Express also enables you to work with .mdf files, but the user instance feature is deprecated; therefore, LocalDB is recommended for working with .mdf files. LocalDB is installed by default with Visual Studio.
Typically, SQL Server Express is not used for production web applications. LocalDB in particular is not recommended for production use with a web application because it's not designed to work with IIS.
Programming Entity Framework Code First Pdf Free Download Pdf
In this tutorial, you'll work with LocalDB. Open the application Web.config file and add a
connectionStringselement preceding the
appSettingselement, as shown in the following example. (Make sure you update the Web.config file in the root project folder. There's also a Web.config file in the Views subfolder that you don't need to update.)
The connection string you've added specifies that Entity Framework will use a LocalDB database named ContosoUniversity1.mdf. (The database doesn't exist yet but EF will create it.) If you want to create the database in your App_Data folder, you could add
AttachDBFilename= DataDirectory ContosoUniversity1.mdf to the connection string. For more information about connection strings, see SQL Server Connection Strings for ASP.NET Web Applications.
You don't actually need a connection string in the Web.config file. If you don't supply a connection string, Entity Framework uses a default connection string based on your context class. For more information, see Code First to a New Database.
Create controller and views
Now you'll create a web page to display data. The process of requesting the data automatically triggers the creation of the database. You'll begin by creating a new controller. But before you do that, build the project to make the model and context classes available to MVC controller scaffolding.
Right-click the Controllers folder in Solution Explorer, select Add, and then click New Scaffolded Item.
In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity Framework, and then choose Add.
In the Add Controller dialog box, make the following selections, and then choose Add:
Model class: Student (ContosoUniversity.Models). (If you don't see this option in the drop-down list, build the project and try again.)
Data context class: SchoolContext (ContosoUniversity.DAL).
Controller name: StudentController (not StudentsController).
Leave the default values for the other fields.
When you click Add, the scaffolder creates a StudentController.cs file and a set of views (.cshtml files) that work with the controller. In the future when you create projects that use Entity Framework, you can also take advantage of some additional functionality of the scaffolder: create your first model class, don't create a connection string, and then in the Add Controller box specify New data context by selecting the + button next to Data context class. The scaffolder will create your
DbContextclass and your connection string as well as the controller and views.
Visual Studio opens the ControllersStudentController.cs file. You see that a class variable has been created that instantiates a database context object:
Indexaction method gets a list of students from the Students entity set by reading the
Studentsproperty of the database context instance:
The StudentIndex.cshtml view displays this list in a table:
Press Ctrl+F5 to run the project. (If you get a 'Cannot create Shadow Copy' error, close the browser and try again.)
Click the Students tab to see the test data that the
Seedmethod inserted. Depending on how narrow your browser window is, you'll see the Student tab link in the top address bar or you'll have to click the upper right corner to see the link.
View the database
When you ran the Students page and the application tried to access the database, EF discovered that there was no database and created one. EF then ran the seed method to populate the database with data.
You can use either Server Explorer or SQL Server Object Explorer (SSOX) to view the database in Visual Studio. For this tutorial, you'll use Server Explorer.
Close the browser.
In Server Explorer, expand Data Connections (you may need to select the refresh button first), expand School Context (ContosoUniversity), and then expand Tables to see the tables in your new database.
Right-click the Student table and click Show Table Data to see the columns that were created and the rows that were inserted into the table.
Close the Server Explorer connection.
The ContosoUniversity1.mdf and .ldf database files are in the %USERPROFILE% folder.
Because you're using the
DropCreateDatabaseIfModelChanges initializer, you could now make a change to the
Student class, run the application again, and the database would automatically be re-created to match your change. For example, if you add an
EmailAddress property to the
Student class, run the Students page again, and then look at the table again, you'll see a new
The amount of code you had to write in order for Entity Framework to be able to create a complete database for you is minimal because of conventions, or assumptions that Entity Framework makes. Some of them have already been noted or were used without your being aware of them:
- The pluralized forms of entity class names are used as table names.
- Entity property names are used for column names.
- Entity properties that are named
IDare recognized as primary key properties.
- A property is interpreted as a foreign key property if it's named <navigation property name><primary key property name> (for example,
Studentnavigation property since the
Studententity's primary key is
ID). Foreign key properties can also be named the same simply <primary key property name> (for example,
Enrollmententity's primary key is
Programming Entity Framework Code First Pdf Free Download Free
You've seen that conventions can be overridden. For example, you specified that table names shouldn't be pluralized, and you'll see later how to explicitly mark a property as a foreign key property.
Get the code
For more about EF 6, see these articles:
In this tutorial, you:
- Created an MVC web app
- Set up the site style
- Installed Entity Framework 6
- Created the data model
- Created the database context
- Initialized DB with test data
- Set up EF 6 to use LocalDB
- Created controller and views
- Viewed the database
Code First Entity Framework Mvc
Advance to the next article to learn how to review and customize the create, read, update, delete (CRUD) code in your controllers and views.