Check if the object has links in other objects in Code First

Perhaps you know how I can determine if an object has links to it in other objects or not?

If I speak SQL, I mean, can I check if the primary key is a foreign key in certain tables.

I want to mark the object of the object as IsDeleted (this property), only if it does not have references to it from other tables, I want to avoid physical deletion.

Thanks,

+4
source share
1 answer

For simple cases, you can check for foreign keys using Any operator:

public class Country { public int ID { get; set; } public string Name { get; set; } } public class City { public int ID { get; set; } public int CountryID { get; set; } public string Name { get; set; } } public bool IsCountryReferenced(Country country, IEnumerable<City> cities) { return cities.Any(city => city.CountryID == country.ID); } 
+1
source

Source: https://habr.com/ru/post/1391136/


All Articles