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

后端 未结 4 793
广开言路
广开言路 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 12:01

    Just let it fail. If the entity has many relationships, that verification could be really heavy.

    public bool TryDelete(int id)
    {
        try
        {
            // Delete
            return true;
        }
        catch (SqlException ex)
        {
            if (ex.Number == 547) return false; // The {...} statement conflicted with the {...} constraint {...}
            throw; // other error
        }
    }
    

提交回复
热议问题