A simple MVC5 + EF6 example sharing and mvc5ef6 example sharing
The software and environment used in this article:
Visual Studio Ultimate 2013;
MVC5 + EF6 +. NET Framework 4.5 + LocalDB; Windows 7x64 Professional
Note:
1. InEF(Entity Framework, Hereinafter referred toEF6) There are three methods to operate data under the framework:Database First, Model First, AndCode First, This article is based onCode FirstCreate.
2. This Article is based onMVC5Create:
3. LocalDB
- LocalDBYesSQL Server ExpressThe lightweight version of the database engine, which is very easy to install, configure, start and run onUser model.
- LocalDBToSQL Server ExpressRun a Special execution model so that you can. MdfFile to operate the database. If you want to make the database have the ability to migrate with the project, you canLocalDBStore database filesWebProjectApp_DataFolder.
- InSQL Server ExpressAlthough you can use the user example function to perform operations. MdfBut this approach is not recommended. On the contrary,LocalDBIs recommended. InVisual Studio2012And later versions,LocalDBWithVisualStudio is installed by default.
- SQLServer ExpressIt is not usedWebApplication production environment, similarly,LocalDBBecause it is notIISThe design is not recommended for use in the production environment.
I. CreateMVCWeb Application
Before getting started, let's take a look.VS 2013Is the startup interface a bit cool?
Let's get down to the point. First, create
After the website is created, we make some minor adjustments to the website style to fit the application topic.
Views \ Shared \ _ Layout. cshtmlMake the following changes (see the highlighted yellow part)
<! DOCTYPE html>
Run the command to check the effect.
Install EF6
Create a data model
InModelsFolder, createContact. cs, Enrollment. cs, Group. csThree Types
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<Enrollment> 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<Enrollment> Enrollments { get; set; } }}
PS: FoundVS 2013There is an automatic promptReferenceIs it very convenient?
CreateDatabase Context
InPCT. ContactCreate a folder under the projectDAL (Data Access Layer), And then continue to createCommunicationContext. cs
Tragedy: Because the class Contact and Project Name Contact are repeated, you have to write the full name. Pay attention to it later.
ContinueDALCreate under directoryCommunicationInitializer. cs
For NotificationEFUseInitializer class,InWeb. configAddEntityFrameworkNode
<entityFramework> <contexts> <context type="PCT.Contact.DAL.CommunicationContext, PCT.Contact"> <databaseInitializer type="PCT.Contact.DAL.CommunicationInitializer, PCT.Contact" /> </context> </contexts> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>
In the projectWeb. configAddConnectionstrings(InAppSettingsAbove)
<connectionStrings> <add name="CommunicationContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContactCommunication;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
Running result
ViewLocalDB
I hope this article will help you learn more.