A simple MVC5 + EF6 example sharing and mvc5ef6 example sharing

Source: Internet
Author: User
Tags sql server express actionlink

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.