Skip to content

Instantly share code, notes, and snippets.

@fpopic
Created June 24, 2015 17:43
Show Gist options
  • Select an option

  • Save fpopic/2425b5511446f429ee93 to your computer and use it in GitHub Desktop.

Select an option

Save fpopic/2425b5511446f429ee93 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
namespace Model
{
public class BoatDAL
{
public List<Boat> FetchAll()
{
using (var ctx = new RPPP14Entities())
{
return ctx.Boats.ToList();
}
}
public Boat Fetch(int boatId)
{
using (var ctx = new RPPP14Entities())
{
return ctx.Boats.SingleOrDefault(b => b.Id == boatId);
}
}
public void Insert(Boat newBoat)
{
using (var ctx = new RPPP14Entities())
{
ctx.Boats.Add(newBoat);
ctx.SaveChanges();
}
}
public void Update(Boat newBoat)
{
using (var ctx = new RPPP14Entities())
{
//izbrisi stari sa istim id
var oldBoat = ctx.Boats.Single(boat => boat.Id == newBoat.Id);
ctx.Boats.Remove(oldBoat);
//dodaj novi na isti id
ctx.Boats.Add(newBoat);
ctx.SaveChanges();
}
}
public void Delete(int id)
{
using (var ctx = new RPPP14Entities())
{
var b = ctx.Boats.Single(boat => boat.Id == id);
ctx.Boats.Remove(b);
ctx.SaveChanges();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment