LINQ to SQL: Self-regulatory Object - ParentID / ChildID Association

I have a Task object in my Linq to SQL dbml. He himself refers to the columns ID and ParentID. I have an association that binds two identifiers together.

Everything seems to work well in intellisense. This will allow me to print Task.Parent.ID and even Task.Parent.Parent.ID etc. However, this gives me the ol link, "Link to an object not set to an instance of the object." mistake.

All my other associations work fine with my other entities. Only object self-regulation errors.

Is there anything special I need to do to get it working, or is it better for me to just add a second Task object and call its ParentTask and make the association this way?

+3
source share
2 answers

An error will occur if you try to get the parent of the object, if in fact it does not have a parent. Everything that you indicated is fine, and I have repeatedly applied this kind of relationship.

You mentioned the exception, but did not mention when this will happen. During the upgrade? Perhaps there is a violation of FC? During INsert? Or just a request?

Task.Parent.Parent.Parent.Parent is not something you can just do endlessly. Of course, it compiles, the code does not know that there is data supporting this operator. But at runtime, each .Parent call must be matched by an equivalent ParentID column.

+4
source

LINQ to SQL. , , , (Item.Parent.ID, Item.Parent.Parent.Parent.Parent.ID ..).

SQL , SQL- LINQ. designer.cs , Parent Task.

0

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


All Articles