1. 使用NuGet添加EF6
2. 添加实体Model publicclassUser_Model{publicintID{get;set;}publicstringS_UserID{get;set;}publicintSex{get;set;}publicstringPhone{get;set;}publicintAge{get;set;}publicDateTime?AddDateTime{get;set;}publicDateTime?UpdDateTime{get;set;}}3. 创建Map(Model成员约束) publicclassUser_Map:EntityTypeConfiguration<Model.User_Model>{publicUser_Map(){ToTable("3660_kangwl.Users");//自定义表名this.HasKey(u=>u.ID);//主键this.PRoperty(u=>u.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);//自增张字段this.Property(u=>u.AddDateTime).HasColumnName("Add_DateTime").IsRequired();this.Property(u=>u.S_UserID).HasColumnName("S_User_ID");this.Property(u=>u.UpdDateTime).HasColumnName("Update_DateTime").IsOptional();}}4. 创建数据库上下文 Context publicclassSmile:DbContext{publicSmile():base(){Database.Connection.ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["smile"].ConnectionString;Database.SetInitializer(newNullDatabaseInitializer<Smile>());}publicDbSet<Model.User_Model>User{get;set;}protectedoverridevoidOnModelCreating(DbModelBuildermodelBuilder){modelBuilder.Configurations.Add(newUser_Map());base.OnModelCreating(modelBuilder);}}5. 操作代码 classProgram{privatestaticvoidMain(string[]args){Smilesmile=newSmile();varusers=smile.User;foreach(User_Modeluserinusers){Console.WriteLine(user.S_UserID);}//由于加了实体与数据库字段映射//下面的sql查询用了as转换成映射字段,否则取不出来的//切记,切记//Add_DateTime(数据库字段),AddDateTime(映射字段)varrawSqlUsers=smile.Database.SqlQuery<Model.User_Model>("selectid,s_user_idasS_UserID,Sex,Phone,Age,Add_DateTimeasAddDateTime,update_datetimeasUpdDateTimefromusers");foreach(User_ModeluserinrawSqlUsers){Console.WriteLine(user.ID);}//command指令如下//参数id=@p0intret=smile.Database.ExecuteSqlCommand("updateuserssetage=-1whereid=@p0",2);Console.WriteLine(ret>0);//两个参数intret1=smile.Database.ExecuteSqlCommand("updateuserssetage=-1whereid=@p0ands_user_id=@p1",12,"kangwl");Console.WriteLine(ret>0);varauser=smile.Database.SqlQuery<Model.User_Model>("selectid,s_user_idasS_UserID,Sex,Phone,Age,Add_DateTimeasAddDateTime,update_datetimeasUpdDateTimefromuserswhereid=@p0",12);Console.WriteLine(auser.First().Age);Console.Read();}}6. show 一下配置文件<?xmlversion="1.0"encoding="utf-8"?><configuration><configSections><!--FormoreinformationonEntityFrameworkconfiguration,visithttp://go.microsoft.com/fwlink/?LinkID=237468--><sectionname="entityFramework"type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"requirePermission="false"/></configSections><connectionStrings><addname="smile"connectionString="server=smile.xxx.xxx;database=3660_smile;uid=3660_kangwl;pwd=xxxxx;"providerName="System.Data.SqlClient"/></connectionStrings><entityFramework><defaultConnectionFactorytype="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"><parameters><parametervalue="v11.0"/></parameters></defaultConnectionFactory><providers><providerinvariantName="System.Data.SqlClient"type="System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer"/></providers></entityFramework></configuration>来自为知笔记(Wiz)新闻热点
疑难解答