// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data.Entity; using System.Linq; namespace DBCache.DAL { public class MyDbContext : DbContext { public MyDbContext(string constrkey) : base(constrkey) { } public DbSet Big { get; set; } public DbSet Counter { get; set; } } public class DAL_EF : IDAL { private string constrkey; public DAL_EF(string constrkey) { this.constrkey = constrkey; } public Big GetBig(int id) { Big res = null; using(MyDbContext db = new MyDbContext(constrkey)) { res = db.Big.FirstOrDefault(o => o.Id == id); } return res; } public int? GetCounter(String id) { int? res = null; using(MyDbContext db = new MyDbContext(constrkey)) { Counter temp = db.Counter.FirstOrDefault(o => o.Id == id); if(temp != null) { res = temp.Val; } } return res; } public void IncrementCounter(String id) { using(MyDbContext db = new MyDbContext(constrkey)) { Counter temp = db.Counter.FirstOrDefault(o => o.Id == id); temp.Val = temp.Val + 1; db.SaveChanges(); } } public void Close() { } } }