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" "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