一、前言 久聞EF大名,之前做C/S產品用的是Dapper對SqlLite進行ORM。然後接觸公司授權系統後發現用的是EntityFramework對SQLSever進行ORM。授權系統里用的是DBFirst,增刪查改使用Linq To Entity,覺得非常方便。本篇篇幅較短,老司機可直接略過 二 ...
一、前言
久聞EF大名,之前做C/S產品用的是Dapper對SqlLite進行ORM。然後接觸公司授權系統後發現用的是EntityFramework對SQLSever進行ORM。授權系統里用的是DBFirst,增刪查改使用Linq To Entity,覺得非常方便。本篇篇幅較短,老司機可直接略過
二、添加EF
Step1:添加“新建項”,起個名稱,添加ADO.NET實體數據模型;
Step2:選擇模型類型,來自資料庫的EF設計器;
Step3:選擇數據連接,新建連接,選擇要使用的資料庫類型;預設SQLSever;
Step4:測試連接資料庫;
Step5:選擇EF版本;
Step6:選擇要實體化的表,點擊完成。
三、操作
經過上述操作會產生三個文件:
1. EntityModel.Context.tt (上下文,所有class的DBSet集合都在這個文件下的.cs文件中)
2. EntityModel.tt (每個表映射後的class都放在這個文件下麵)
3. EntityModel.edmx (可視化的表設計器)
假設連接的資料庫下有三個表:AgencyInfo,ContractInfo,CustomerInfo。那麼EntityModel.tt下就會有三個對應的.cs文件:
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; namespace HHH { public partial class AgencyInfo { public System.Guid ID { get; set; } public string UnitName { get; set; } public string Phone { get; set; } public string Address { get; set; } public string comments { get; set; } public Nullable<System.DateTime> CreatTime { get; set; } public Nullable<int> ShowFlag { get; set; } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; namespace HHH { public partial class ContractInfo { public System.Guid ID { get; set; } public string Title { get; set; } public string Comment { get; set; } public Nullable<System.DateTime> CreateDate { get; set; } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; namespace HHH { public partial class CustomerInfo { public System.Guid ID { get; set; } public string Name { get; set; } public string ContactInfo { get; set; } public string Address { get; set; } public string Comments { get; set; } public string Email { get; set; } public string MobilePhone { get; set; } public string province { get; set; } public string City { get; set; } public string Type { get; set; } public Nullable<System.DateTime> CreateDate { get; set; } } }
那麼EntityModel.Context.tt是這樣的:
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; namespace HHH { public partial class MyEntities: DbContext { public MyEntities() : base("name=MyEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<ContractInfo> ContractInfoes { get; set; } public DbSet<CustomerInfo> CustomerInfoes { get; set; } public DbSet<AgencyInfo> AgencyInfoes { get; set; } } }
需要對錶對象操作時,首先要:
private MyEntities dbContext = new MyEntities();
需要對哪個表操作,就dbContext.ContractInfoes或者dbContext.CustomerInfoes這樣找到數據集合,然後用Linq去操作,最後別忘dbContext.SaveChanges()保存修改即可。