public IList <Customer> GetRecords() { using (var db = new YourDatabase()) { var list = (from c in db.Customers select c).ToList(); return(list); } }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var dbPath = Path.Combine(env.ContentRootPath, "App_Data", "ProgCoreEf.mdf"); YourDatabase.ConnectionString = !env.IsDevelopment() ? "production" : String.Format("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename={0};Initial Catalog=ProgCore_Ef;Integrated Security=True", dbPath); app.UseDeveloperExceptionPage(); app.UseMvcWithDefaultRoute(); var db = new YourDatabase(); db.Database.EnsureCreated(); //db.EnsureSeedData(); }
public void Save(Customer customer) { using (var db = new YourDatabase()) { var existing = (from c in db.Customers where c.Id == customer.Id select c).SingleOrDefault(); if (existing == null) { db.Customers.Add(customer); } else { existing.FirstName = customer.FirstName; existing.LastName = customer.LastName; } db.SaveChanges(); } }