Answered by:
How check if an Entity has related dependent

Question
-
Hi, some time ago I was using ObjectContext/EntityObject but I am currently migrating to dbContext.
With EntityObject used a function that takes in the following post.
Could you get the same information now that my entities do notinherit from EntityObject.
Thanks
Saturday, May 23, 2015 5:08 PM
Answers
-
Hello Alejandro,
>>Could you get the same information now that my entities do notinherit from EntityObject.
For achieving what you want, it is not clear what approach you are using(Database first? Code first?). If you uses code first approach, you need to make sure your entities meet requirements for Creating POCO Proxies: https://msdn.microsoft.com/en-us/library/dd468057(v=vs.100).aspx, these proxies are actually would be similar the EntityObject type which implements the IEntityWithChangeTracker or IEntityWithRelationships interfaces and you could modify the extend method as a static method and pass the entity object to check its state as:
namespace CFs { class Program { static void Main(string[] args) { try { using (SC20150430 db = new SC20150430()) { //db.Database.CreateIfNotExists(); db.Configuration.ProxyCreationEnabled = true; var order = db.Posts.FirstOrDefault(); var neworder = order as IEntityWithRelationships; // Also get NULL Console.WriteLine(HasDeleteConstraints(neworder)); } #endregion } catch (Exception) { throw; } } public static bool HasDeleteConstraints(object obj) { // Get all related ends IEnumerable<IRelatedEnd> relEnds = ((IEntityWithRelationships)obj).RelationshipManager.GetAllRelatedEnds(); foreach (IRelatedEnd relEnd in relEnds) { RelationshipEndMember rsem = relEnd.RelationshipSet.ElementType.RelationshipEndMembers[relEnd.SourceRoleName]; if (rsem.RelationshipMultiplicity == RelationshipMultiplicity.One || rsem.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne) { if (rsem.DeleteBehavior != OperationAction.Cascade) { IEnumerable collection = relEnd.CreateSourceQuery(); foreach (var item in collection) { return true; } } } } return false; } } }
If you are using the database first approach, since these entity are generated by the T4 template, you need to customize the T4 template to let it generate POCO entity class for you: https://msdn.microsoft.com/en-us/data/gg558520.aspx and use above code to get the entity state similarly.
Regards
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by Fred Bao Monday, June 1, 2015 9:35 AM
Monday, May 25, 2015 6:28 AM
All replies
-
Hello Alejandro,
>>Could you get the same information now that my entities do notinherit from EntityObject.
For achieving what you want, it is not clear what approach you are using(Database first? Code first?). If you uses code first approach, you need to make sure your entities meet requirements for Creating POCO Proxies: https://msdn.microsoft.com/en-us/library/dd468057(v=vs.100).aspx, these proxies are actually would be similar the EntityObject type which implements the IEntityWithChangeTracker or IEntityWithRelationships interfaces and you could modify the extend method as a static method and pass the entity object to check its state as:
namespace CFs { class Program { static void Main(string[] args) { try { using (SC20150430 db = new SC20150430()) { //db.Database.CreateIfNotExists(); db.Configuration.ProxyCreationEnabled = true; var order = db.Posts.FirstOrDefault(); var neworder = order as IEntityWithRelationships; // Also get NULL Console.WriteLine(HasDeleteConstraints(neworder)); } #endregion } catch (Exception) { throw; } } public static bool HasDeleteConstraints(object obj) { // Get all related ends IEnumerable<IRelatedEnd> relEnds = ((IEntityWithRelationships)obj).RelationshipManager.GetAllRelatedEnds(); foreach (IRelatedEnd relEnd in relEnds) { RelationshipEndMember rsem = relEnd.RelationshipSet.ElementType.RelationshipEndMembers[relEnd.SourceRoleName]; if (rsem.RelationshipMultiplicity == RelationshipMultiplicity.One || rsem.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne) { if (rsem.DeleteBehavior != OperationAction.Cascade) { IEnumerable collection = relEnd.CreateSourceQuery(); foreach (var item in collection) { return true; } } } } return false; } } }
If you are using the database first approach, since these entity are generated by the T4 template, you need to customize the T4 template to let it generate POCO entity class for you: https://msdn.microsoft.com/en-us/data/gg558520.aspx and use above code to get the entity state similarly.
Regards
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by Fred Bao Monday, June 1, 2015 9:35 AM
Monday, May 25, 2015 6:28 AM -
OK, I'm using Database First, thanks for the help.Wednesday, May 27, 2015 2:14 AM