我用這個來實現同樣的事情......並且我正在使用Entity Framework 5.我知道Using()塊現在可以使用並且被推薦 - 但是這裏有一些基本的命令只適用於我添加一些替代品:
/// <summary>
/// Method that will remove a favorite from the tblfavorite table.
/// </summary>
/// <param name="favoriteID"></param>
/// <returns></returns>
public Boolean DeleteFavoriteByFavoriteID(Int32 favoriteID)
{
//Assume not found.
IsFound = false;
//Query the DB.
var MatchedRec = (from f in dbContext.tblfavorites
where f.FavoriteID == favoriteID
select f).FirstOrDefault();
//See if anything was found.
if (MatchedRec != null)
{
IsFound = true;
dbContext.tblfavorites.Remove(MatchedRec);
dbContext.SaveChanges();
return true;
}
//Default.
return false;
}