简体   繁体   English

SQL Server关系中一个非常有趣的问题(SQL Server 2008R2,LINQ To SQL,Visual Studio 2010,C#)

[英]A very interesting problem with SQL Server Relationships (SQL Server 2008R2, LINQ To SQL, Visual Studio 2010, C#)

I'm working on a web project. 我正在做一个网络项目。 I'm using a Sql Server 20008 R2 database via LINQ To SQL, and today I faced a very strange problem with relationships. 我通过LINQ To SQL使用Sql Server 20008 R2数据库,今天我遇到了一个非常奇怪的关系问题。 I've been using them for a while and never had any problems so far. 我使用它们已有一段时间了,到目前为止没有任何问题。

Let's say I have a table Stores that has the following fields: ID , Name , LastUserID where LastUserID is a "reference" to the Users table. 假设我有一个表Stores ,该表Stores具有以下字段: IDNameLastUserID ,其中LastUserID是对Users表的“引用”。

The Users table has the following fields: ID , Name , FavoriteStoreID where FavoriteStoreID is a "reference" to the Stores table. Users表具有以下字段: IDNameFavoriteStoreID ,其中FavoriteStoreID是对Stores表的“引用”。

So I kind of have recursive relationship. 所以我有点递归的关系。

Store.LastUser.Name // shows the name of the last user of the store

User.FavoriteStore.Name // shows the name of user's favorite store.

In Visual Studio's designer this looks like this: 在Visual Studio的设计器中,这看起来像这样:

----------
-        -
- Users  -
-        -
----------


 |   / \
 |    |
 |    |
\ /   |


----------
-        -
- Stores -
-        -
----------

Now the problem is, when there are two arrows (two relationships), only one of them works. 现在的问题是,当有两个箭头(两个关系)时,只有其中一个起作用。 When I use the other, I get the Object reference not set to an instance of an object. 当我使用另一个Object reference not set to an instance of an object. ,我得到的Object reference not set to an instance of an object. error. 错误。

When I re-create the table, add that relationship (the one that raised an exception) first, it works. 当我重新创建表时,首先添加该关系(引发异常的关系),它会起作用。 But when I add another one, it doesn't work. 但是,当我添加另一个时,它不起作用。

So obviously I messed up something, and SQL Server doesn't understand how to interpret what I want it to do. 所以很明显,我弄乱了一些东西,而SQL Server不理解如何解释我想要它做什么。

How can I fix this problem? 我该如何解决这个问题?

The problem is that the designer can't figure out the circular reference. 问题在于设计人员无法弄清楚循环参考。 I would handle this by creating the missing reference inside the designer. 我将通过在设计器内部创建缺少的引用来处理此问题。

It sounds like you're setting up two entity associations when you only need one, and that may be confusing LINQ to SQL. 听起来好像您只需要一个时就建立了两个实体关联,这可能会使LINQ与SQL混淆。 In your dbml designer try setting up only one association between your Users and Stores tables, and in the the association properties, set the cardinality to OneToOne and that should create a Child Property and Parent Property on your User and Store entities. 在您的dbml设计器中,尝试仅在“用户”和“商店”表之间建立一个关联,然后在关联属性中,将基数设置为OneToOne,这将在“用户”和“商店”实体上创建子属性和父属性。 Even with only one association between the two tables, you should still be able to get the Store from the User object and the User from the Store object. 即使两个表之间只有一个关联,您仍应该能够从User对象获得Store,并从Store对象获得User。

I'm not sure what was the problem, but I re-created the table, and now it works fine. 我不确定是什么问题,但是我重新创建了表,现在工作正常。 So LINQ To SQL DOES work with circular references. 因此LINQ到SQL 确实与循环引用的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM