Entityframwork creating a framework and database connection manually

Source: Internet
Author: User

Now I started to study entityframwork, originally in the vs2012 through the tool to create EF, but for me this unfamiliar rookie has a lot of business with EF to make or a bit difficult, today to manually build an EF framework, God do not spray

First: Create a Class menu reference namespace because the table requires a foreign key to map to the primary key

Using System.ComponentModel.DataAnnotations;

Add a unique identifier on the primary key when creating the class [Key],ef Otherwise, the execution will error

  Public Partial classMenu {[Key] Public intMId {Get;Set; }  Public intMPId {Get;Set; }  Public stringMname {Get;Set; }  Public stringMIco {Get;Set; }  Public stringMLink {Get;Set; }  Public intMsort {Get;Set; }  Public stringMremark {Get;Set; }  Public BOOLMisshow {Get;Set; }  Public BOOLMisdel {Get;Set; }  PublicSystem.DateTime Maddtime {Get;Set; } }

Step two: Create an EF context object

 Public  class Dbcontextfactory:dbcontext    {              protectedoverridevoid  onmodelcreating (Dbmodelbuilder modelBuilder)        {            modelBuilder.Conventions.Remove<PluralizingTableNameConvention> ();   Remove   contract for plural table name         }              publicgetset;}    } 

Step Three: Configure Webconfig the first configuration will appear "Unsupported keyword:" Data source "this is because metadata=res://*/model.csdl|res://*/model.ssdl|res://*/ MODEL.MSL the name of the corresponding namespace in this statement

<add name="DbContextFactory1" connectionstring="metadata=res://*/ Model.csdl|res://*/model.ssdl|res://*/model.msl;provider=system.data.sqlclient;provider Connection string=& Quot;data source=.; Initial Catalog=dbdata;user id=sa;password=123456789; Multipleactiveresultsets=true; app=entityframework&quot;  "providername="System.Data.EntityClient "/>
View Code

Then the problem occurs " Unable to load the specified metadata resource" and then modifies the data configuration for the second time

<add name= "Dbcontextfactory" connectionstring= "server=.;D Atabase=dbdata; Uid=sa; pwd=123456789; Pooling=true; Connection lifetime=300;packet size=1000 "providername=" System.Data.SqlClient "/>

After modifying the configuration file for the second time, manually creating the EF framework to complete, not tight can use EF simple business logic, if not familiar can also add complex data layer operations on this basis, note: Webconfig configuration page Name should match the names of the EF context objects you created

Entityframwork creating a framework and database connection manually

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.