问题描述
我在我的LINQ to SQL的dbml任务实体。这是自参照与ID和PARENTID列。我有关联的两个ID一起的关联。
I have a Task entity in my Linq to SQL dbml. It's self referencing with ID and ParentID columns. I have an Association that associates the two IDs together.
好像一切工作正常,在智能感知。这将让我输入了Task.Parent.ID甚至Task.Parent.Parent.ID,等等。但是,它给我的醇'对象未设置为一个对象的实例。错误。
it seems like everything works fine in the intellisense. It'll let me type out Task.Parent.ID and even Task.Parent.Parent.ID, etc. However, it gives me the ol' "Object reference not set to an instance of an object." error.
我所有其它协会正常工作与我的其他实体。仅自参考实体错误。
All my other associations work fine with my other entities. Only the self referencing entity errors.
有什么特别的,我需要做的就是它的工作还是我最好只添加第二个任务实体,并呼吁它ParentTask并进行关联呀?
Is there something special I need to do to get it to work or am I better off just adding a second Task entity and call it ParentTask and make the association that way?
推荐答案
错误,如果你试图获取父实体时,它其实并没有一个家长会发生。您所指定的一切是完全没有问题,我已经实现了类似的关系多次。
The error will happen if you are attempting to get the Parent for an entity when it in fact does not have a Parent. Everything you have specified is perfectly fine and I have implemented similar relationships many times.
您提到一个异常,但是当它发生时你没有提到。在更新过程中?或许有一个FK违规行为?在一个INSERT?或者只是一个查询?
You mentioned an exception but you did not mention when it occurs. During an update? Perhaps there is a FK violation? During an INsert? Or just a query?
Task.Parent.Parent.Parent.Parent是不是你可以做无限的东西。当然它会编译时,code不知道,有数据备份的发言。但在运行时,每个.Parent呼叫需要由一个等效PARENTID列匹配
Task.Parent.Parent.Parent.Parent is not something you can just do infinitely. Sure it will compile, the code doesn't know that there is data backing up that statement. But at runtime, each .Parent call needs to be matched by an equivalent ParentID column.
这篇关于LINQ to SQL的:自引用的实体 - PARENTID / ChildID协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!