C#, EF6, 建立資料庫DBEntity物件
public class MyDBEntities : DbContext { public MyDBEntities() : base("name=DBConnectionName") { } public DbSet<Table1> Table1 { get; set; } public DbSet<Table2> Table2 { get; set; } //取得每個DbSet的內容 public IQueryable<TEntity> GetALL<TEntity>() where TEntity : class { return Set<TEntity>().AsQueryable(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //讓Entity Framework啟動不再檢查__MigrationHistory表 Database.SetInitializer<MyDBEntities>(null); base.OnModelCreating(modelBuilder); } }
使用範例
Lambda
using (GeneralEntities EPEntities = new GeneralEntities()) { var IQuery_Table1 = EPEntities.GetALL<Table1>(); List<Table1> IQuery_Table1 = IQuery_Table1.Where(x => x.AA == "123").ToList(); }
LINQ
using (GeneralEntities EPEntities = new GeneralEntities()) { var IQuery_Table1 = from t1 in EPEntities.Table1; List<Table1> IQuery_Table1 = IQuery_Table1.Where(x => x.AA == "123").ToList(); }
留言
張貼留言