【发布时间】:2010-10-09 16:14:39
【问题描述】:
我正在做一个网络项目。我正在通过 LINQ To SQL 使用 Sql Server 20008 R2 数据库,今天我遇到了一个非常奇怪的关系问题。我已经使用它们一段时间了,到目前为止从未遇到任何问题。
假设我有一个表Stores,其中包含以下字段:ID、Name、LastUserID 其中LastUserID 是对Users 表的“引用”。
Users 表具有以下字段:ID、Name、FavoriteStoreID 其中FavoriteStoreID 是对 Stores 表的“引用”。
所以我有点递归关系。
Store.LastUser.Name // shows the name of the last user of the store
User.FavoriteStore.Name // shows the name of user's favorite store.
在 Visual Studio 的设计器中,如下所示:
----------
- -
- Users -
- -
----------
| / \
| |
| |
\ / |
----------
- -
- Stores -
- -
----------
现在的问题是,当有两个箭头(两个关系)时,只有一个有效。当我使用另一个时,我收到Object reference not set to an instance of an object. 错误。
当我重新创建表时,首先添加该关系(引发异常的那个),它可以工作。但是当我添加另一个时,它就不起作用了。
很明显我搞砸了,SQL Server 不明白如何解释我想要它做的事情。
我该如何解决这个问题?
【问题讨论】:
标签: c# sql-server linq-to-sql relationship