Skip to content

Instantly share code, notes, and snippets.

@fpopic
Created June 24, 2015 16:39
Show Gist options
  • Select an option

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

Select an option

Save fpopic/a42ee70cdc5514b4796f 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 Boat Fetch(string boatName)
{
using (var ctx = new RPPP14Entities())
{
return ctx.Boats.SingleOrDefault(b => b.BoatName.Equals(boatName));
}
}
public void Insert(int id, string boatName, int countryId, decimal length, int ownerId)
{
using (var ctx = new RPPP14Entities())
{
var b = new Boat
{
Id = id,
BoatName = boatName,
CountryId = countryId,
Length = length,
OwnerId = ownerId
};
ctx.Boats.Add(b);
ctx.SaveChanges();
}
}
public void Update(int id, string boatName, int countryId, decimal length, int ownerId)
{
using (var ctx = new RPPP14Entities())
{
var b = ctx.Boats.Single(boat => boat.Id == id);
b.BoatName = boatName;
b.CountryId = countryId;
b.Length = length;
b.OwnerId = ownerId;
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