NorthwindEntities ctx = new NorthwindEntities();
// Retrieves USERS
var entity = ctx.USERS().ToList();
// Retrieve data including its related entities
public IList<E> Retrieve()
{
var data = entity;
foreach (var relatedEntity in (((IEntityWithRelationships)data.FirstOrDefault()).
RelationshipManager.GetAllRelatedEnds()))
{
entity.Include(relatedEntity.TargetRoleName).ToList();
}
return entity.ToList();
}
// Retrieves specific USER
var _users = ctx.USERS().Where(x=>x.USER_ID == 1).FirstOrDefault();
// Delete the entity
public void Delete(bool ClearNavProperties) {
_ctx.DeleteObject(_users);
DeleteRelatedEntries(ClearNavProperties);
}
// Delete each related entities
void DeleteRelatedEntries(bool ClearNavProperties)
{
if (ClearNavProperties == true) {
foreach (var relatedEntity in (((IEntityWithRelationships)entity).
RelationshipManager.GetAllRelatedEnds().SelectMany(re =>
re.CreateSourceQuery().OfType<EntityObject>()).Distinct()).ToArray())
{
ctx.DeleteObject(relatedEntity);
}
}
}
Note : You can also edit the edmx and add scripts for automatic retrieval and delete
of the related entities.