如何使用实体框架5进行级联删除

时间:2015-11-10 09:26:48

标签: c# entity-framework

我正在使用实体集的删除对象来删除GridView中有效的行,但它们是我要清除的子项

if (e.CommandName == "Delete")
{
     GridDataItem item = e.Item as GridDataItem;
     Guid strId = new Guid(item.GetDataKeyValue("id").ToString());

     player _player = _dal.GetPlayerBYID(strId);

     _dal.SoccerEntities.players.DeleteObject(_player);

     _dal.SoccerEntities.SaveChanges();
     grdSoccerPlayers.DataBind();
}

我使用的查询是收集玩家的以下内容

public player GetPlayerBYID(Guid  _playerId)
{
     try
     {
         if (_playerId == Guid.Empty)
         {
              player _player = new player();
              _player.player_id = "16-56-232";
              return _player;
         }
         else
         {
              var q = SoccerEntities.players.Where(p => p.id == _playerId);

              if (q == null)
                 throw new EntityContextException(string.Format("A player could not be found {0}!", _playerId));
              else
                 return q.ToList()[0];
         }
     }
     catch (Exception ex)
     {
          throw new EntityContextException("GetPlayerBYID failed.", ex);
     }     
}

但是它有一个更高的表名为team,它们是一个teamId链接两个表。我相信我需要的是级联删除,但这是否适用于实体框架?

0 个答案:

没有答案