MVC5 + EF6 Simple Example

Source: Internet
Author: User
Tags button type windows 7 x64 sql server express actionlink connectionstrings

    • Visual Studio Ultimate (:http://www.visualstudio.com/downloads/download-visual-studio-vs);
    • MVC5 + EF6 +. NET Framework 4.5 + LocalDB;
    • Windows 7 x64 Professional

Description

  1. There are three ways to manipulate data in the EF (Entity Framework, hereinafter referred to as EF6) Framework: Database First,Modelfirst, and CodeFirst, this article was created based on code first. More about EF6 please refer to http://msdn.microsoft.com/en-us/data/ef.aspx;
  2. This article is based on MVC5 creation, more about its description and use please refer to HTTP://WWW.ASP.NET/MVC;
  3. LocalDB:
    • LocalDB is a lightweight version of the SQL Server Express database engine, which is very easy to install, configure, start and run on the command line in the user model.
    • LocalDB runs in a special SQL Server Express execution model, allowing you to manipulate the database in the same way as . mdf files. If you want to make the database capable of migrating with the project, you can put the LocalDB database file under the App_Data folder of the Web project.
    • In SQL Server Express , although you can achieve the purpose of manipulating . mdf files by using the User Sample feature, however this practice is not recommended, on the contrary,LocalDB is the recommended way. In Visual Studio and subsequent releases,LocalDB is installed by default with Visual Studio.
    • In general, SQL Server Express is not used in a production environment for Web applications, asLocalDB is not intended for IIS The design is not recommended for use in production environments.

First, create an MVC -based Web application

Before the official start, take a look at the launch interface of VS , is not a bit cold feeling

Okay, so let's start by creating the following

Once created, we fine-tune the style of the site to fit the application theme

views\shared\_layout.cshtml make the following changes (see the yellow highlight section)

<! DOCTYPE html>

  views\home\index.cshtml replaced with the following:

@{    viewbag.title = "Home page";} <div class= "Jumbotron" >    

Run and look at the effect.

Installing EF6

Creating a data Model

Under the Models folder, create Contact.cs, Enrollment.cs, Group.cs three classes respectively

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: found that the VS has an automatic hint reference, is not very convenient AH

CreateDatabase Context

  In the PCT. Create a new folder DAL (Data Access Layer)under the contact item, and continue with the new CommunicationContext.cs

Tragedy Ah, because the class contact and project name contact duplicate, have to write full name Ah, later attention.

Continue to create CommunicationInitializer.cs under the DAL directory

To notify EF to use the initializer class you created, add the entityframework node to the project's Web. config  

  <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>
Set upEFUseSQL Server Express LocalDB database

Add connectionstrings(above appSettings ) in Project Web. config

  <connectionStrings>    <add name= "Communicationcontext" connectionstring= "Data source= (LocalDb) \v11.0;i Nitial 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>
Create ContactThe Controller and view

  

Run results

ViewLocalDB

This article the original length

This article original address: http://panchunting.cnblogs.com/

MVC5 + EF6 Simple Example

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.