开发者

Dynamic Data one-to-many relationships using non-PK fields: Foreign table List.aspx links use incorrect key for one-to-many relationships

Problem: The urls created between tables with one-to-many relationships are incorrect. How can I fix this?

I'm using LINQ to SQL with Dynamic Data.

I have two tables: Users and Addresses:

* There is a one-to-many from Users TO Addresses ie: A user may have one or more addresses. 
* The two tables are NOT linked using their primary keys, but using a separate GUID field.
* The User table has Id (int) as it's PK and 'PrincipalId' (Guid) as the linking key
* The Addresses table has AddressId(int) as it's PK and 'PrincipalId' (Guid) as the linking key
* 'PrincipalId' is unique in the user table.
* The 'Association' looks good in the DataAccess class, and was done using the dbml designer.

Symptoms:

When viewing the User table, I get a column on the end with 'View user addresses' linking like so: http://dev.cityslurp.com/Addresses/List.aspx?PrincipalId=18

Note:

  1. PrincipalId is a GUID, not an int, so I think the '18' should actually be a GUID
  2. The integer maps properly to the User.Id value
  3. So I think the link should look like: http://dev.cityslurp.com/Addresses/List.aspx?PrincipalId=157221EF-C85C-4FDF-B861-60D149E11BFC, but following that give an error: "'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value" - I'm assuming this error is because the dropdown value is expecting an integer.

So even though # 3 gives me an error, I think that it's the correct queryparameter to use.

Conversely, when I go to the Address/List.aspx page, I have a column with links back to the User Details page. The links correctly show the user's email address as the text, but again the link is wrong. For example for the same user, their link FROM the address L开发者_如何转开发ist page is: http://dev.cityslurp.com/Users/Details.aspx?Id=157221ef-c85c-4fdf-b861-60d149e11bfc

Note:

  1. User.Id is the PK for the User table
  2. the User.Id is an integer
  3. The Guid used is the value from the 'PrincipalId' field
  4. Since the link contains the text of the email address, and the Guid is correct it would seem that the relationship is working, it's just that Id is wrong.

I'm assuming that the link should look like: http://dev.cityslurp.com/Users/Details.aspx?Id=70, which works to view the user details.

I'm thinking that either this maybe a bug, but surely others have successfully used one-to-many relationships using non-PK fields before?

The relationships look to be good, in that they are using the fields that I specified.

Here's an example of the relationship in the auto-generated DataAccess file:

[Table(Name="dbo.Address")] public partial class Address : INotifyPropertyChanging, INotifyPropertyChanged {

...

 [Association(Name="Users_Address", Storage="_Users", ThisKey="PrincipalId", OtherKey="PrincipalId", IsForeignKey=true)]
    public Users Users
    {
        get
        {

...

Any ideas of what to do? I know I can create custom List pages for the Users and the Addresses, but I'm looking for a simpler way - perhaps putting an attribute on one of the partial classes to tell them what to do properly.

Cheers, Lance

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜