In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to create MVC5 + EF6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The software and environment used in this paper:
Visual Studio Ultimate 2013
MVC5 + EF6 + .NET Framework 4.5 + LocalDB;Windows 7 x64 Professional
Description:
1. Under the framework of EF (Entity Framework, hereinafter referred to as EF6), there are three ways to manipulate data: Database First, Model First, and Code First. This paper is based on Code First.
two。 This article is based on MVC5:
3.LocalDB
LocalDB is a lightweight version of the SQL Server Express database engine that is very easy to install, configure, start from the command line and run on user model.
LocalDB runs in a special execution model of SQL Server Express, which allows you to manipulate the database in the form of .MDF files. If you want the database to have the ability to migrate with the project, you can put the LocalDB database file in the App_Data folder of the web project.
Although you can use the user example feature to manipulate .MDF files in SQL Server Express, this is not recommended. Instead, LocalDB is the recommended way. In Visual Studio2012 and subsequent versions, LocalDB is installed by default with Visual Studio.
Generally speaking, SQL Server Express is not used in the production environment of Web applications, and similarly, LocalDB is not recommended for use in production environments because it is not designed for IIS.
First, create a MVCWeb Application-based
Before we officially begin, let's take a look at the startup interface of VS 2013. Isn't it a little cold?
All right, let's get back to business. First of all, create the screenshot below.
After the creation, we make some fine-tuning to the style of the website so that it can match the theme of the application.
Views\ Shared\ _ Layout.cshtml make the following changes (see yellow highlight)
ViewBag.Title-Contact@Styles.Render ("~ / Content/css") @ Scripts.Render ("~ / bundles/modernizr") @ Html.ActionLink ("Contact", "Index", "Home", null, new {@ class = "navbar-brand"}) @ Html.ActionLink ("Home", "Index", "Home") @ Html.ActionLink ("About", "About", "Home") @ Html.ActionLink ("Contacts", "Index") Contact) @ Html.ActionLink ("Groups", "Index", "Group") @ RenderBody ()
©@ DateTime.Now.Year-Contact
@ Scripts.Render ("~ / bundles/jquery") @ Scripts.Render ("~ / bundles/bootstrap") @ RenderSection ("scripts", required: false) Views\ Home\ Index.cshtml with the following content @ {ViewBag.Title = "Home Page";} ContactWelcome to Contact
Contact is a sample application thatdemonstrates how to use Entity Framework 6 in anASP.NET MVC 5 web application.
Build it from scratch
You can build the application by following the steps in the tutorial series on the following site.
See the tutorial »
Let's run it and see the effect.
Install EF6
Create a data model
Under the Models folder, create three classes: Contact.cs, Enrollment.cs, and Group.cs
Using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace PCT.Contact.Models {public class Contact {public int ID {get; set;} public string Name {get; set;} public DateTime EnrollmentDate {get; set;} public virtual ICollection Enrollments {get; set;} using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace PCT.Contact.Models {public class Enrollment {public int EnrollmentID {get; set;} public int ContactID {get; set } public int GroupID {get; set;} public virtual Contact Contact {get; set;} public virtual Group Group {get; set;} using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace PCT.Contact.Models {public enum GroupName {Friend, Family, Colleague, Schoolmate, Stranger} public class Group {public int GroupID {get; set;} public GroupName? GroupName {get; set;} public virtual ICollection Enrollments {get; set;}
PS: isn't it convenient to find that VS 2013 has an automatic prompt for reference?
Create Database Context
Create a new folder DAL (Data Access Layer) under the PCT.Contact project, and then continue to create a new CommunicationContext.cs
Unfortunately, due to the repetition of class Contact and project name Contact, I have to write the full name, ah, pay attention to it later.
Continue to create CommunicationInitializer.cs under the DAL directory
To tell EF to use the initializer class you created, add the entityFramework node to the web.config of the project
Add connectionstrings to the project web.config (on top of appSettings)
Running result
View LocalDB
This is the end of "how to create MVC5 + EF6". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.