Entity Framework: Check all relationships of an entity for foreign key use

后端 未结 4 767
广开言路
广开言路 2020-12-29 11:20

I have an entity, let\'s call it CommonEntity that has a primary key used as a foreign key in many other entities. As the application is developed these links w

4条回答
  •  孤城傲影
    2020-12-29 11:54

    You can try this:

    var allrelatedEnds = ((IEntityWithRelationships)ce).RelationshipManager.GetAllRelatedEnds();
    bool hasRelation = false;
    foreach (var relatedEnd in allrelatedEnds)
    {
        if (relatedEnd.GetEnumerator().MoveNext())
        {
            hasRelation = true;
            break;
        }
    }
    
    if (!hasRelation)
    {
        //Delete
    }
    

提交回复
热议问题