EF6 SqlServer 简单例子和支持的原生sql例子

 总体结构如图:

总体结构

1. 使用NuGet添加EF6

2. 添加实体Model

  1.     public class User_Model {
  2.         public int ID { get; set; }
  3.         public string S_UserID { get; set; }
  4.         public int Sex { get; set; }
  5.         public string Phone { get; set; }
  6.         public int Age { get; set; }
  7.         public DateTime? AddDateTime { get; set; }
  8.         public DateTime? UpdDateTime { get; set; }
  9.     }  

3. 创建Map(Model成员约束)

  1.     public class User_Map : EntityTypeConfiguration<Model.User_Model> {
  2.         public User_Map() {
  3.             ToTable("3660_kangwl.Users");//自定义表名
  4.             this.HasKey(=> u.ID);//主键
  5.             this.Property(=> u.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);//自增张字段
  6.             this.Property(=> u.AddDateTime).HasColumnName("Add_DateTime").IsRequired();
  7.             this.Property(=> u.S_UserID).HasColumnName("S_User_ID");
  8.             this.Property(=> u.UpdDateTime).HasColumnName("Update_DateTime").IsOptional();
  9.         }
  10.     }  

4. 创建数据库上下文 Context

  1.     public class Smile : DbContext {
  2.         public Smile() :base() {
  3.             Database.Connection.ConnectionString =
  4.                 System.Configuration.ConfigurationManager.ConnectionStrings["smile"].ConnectionString;
  5.             Database.SetInitializer(new NullDatabaseInitializer<Smile>());
  6.         }
  7.         public DbSet<Model.User_Model> User { get; set; }
  8.  
  9.         protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  10.             modelBuilder.Configurations.Add(new User_Map());
  11.             base.OnModelCreating(modelBuilder);
  12.         }
  13.     } 

5. 操作代码

  1.    class Program
  2.     {
  3.         private static void Main(string[] args) {
  4.             Smile smile = new Smile();
  5.             var users = smile.User;
  6.             foreach (User_Model user in users) {
  7.                 Console.WriteLine(user.S_UserID);
  8.             }
  9.             //由于加了实体与数据库字段映射
  10.             //下面的sql查询用了as转换成映射字段,否则取不出来的
  11.             //切记,切记
  12.             //Add_DateTime(数据库字段),AddDateTime(映射字段)
  13.             var rawSqlUsers =
  14.                 smile.Database.SqlQuery<Model.User_Model>(
  15.                     "select id,s_user_id as S_UserID,Sex,Phone,Age,Add_DateTime as AddDateTime,update_datetime as UpdDateTime from users");
  16.             foreach (User_Model user in rawSqlUsers) {
  17.                 Console.WriteLine(user.ID);
  18.             }
  19.             //command指令如下
  20.             //参数id=@p0 
  21.             int ret = smile.Database.ExecuteSqlCommand("update users set age=-1 where id=@p0", 2);
  22.             Console.WriteLine(ret > 0);
  23.             //两个参数
  24.             int ret1 = smile.Database.ExecuteSqlCommand("update users set age=-1 where id=@p0 and s_user_id=@p1", 12,"kangwl");
  25.             Console.WriteLine(ret > 0);
  26.  
  27.             var auser =
  28.                 smile.Database.SqlQuery<Model.User_Model>(
  29.                     "select id,s_user_id as S_UserID,Sex,Phone,Age,Add_DateTime as AddDateTime,update_datetime as UpdDateTime from users where id=@p0",
  30.                     12);
  31.  
  32.        
  33.             Console.WriteLine(auser.First().Age);
  34.  
  35.             Console.Read();
  36.         }
  37.     }  

6. show 一下配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <configSections>
  4.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  6.   </configSections>
  7.   <connectionStrings>
  8.     <add name="smile" connectionString="server=smile.xxx.xxx;database=3660_smile;uid=3660_kangwl;pwd=xxxxx;" providerName="System.Data.SqlClient"/>
  9.   </connectionStrings>
  10.   <entityFramework>
  11.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  12.       <parameters>
  13.         <parameter value="v11.0" />
  14.       </parameters>
  15.     </defaultConnectionFactory>
  16.     <providers>
  17.       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  18.     </providers>
  19.   </entityFramework>
  20. </configuration> 
  21.  

扫一扫手机访问