本文介绍了ASP.NET成员资格提供程序:定义一个到aspnet_Users.UserName的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个外键给一个默认的成员表,但是当我尝试定义它的时候出现错误。我使用默认的aspnet_Users表和我自己的Posts表。我试图设置表如下:

aspnet_Users




  • UserId (PK)uniqueidentifier

  • 用户名nvarchar(256)


    帖子




    • PostID(PK)int

    • 用户名(FK-> aspnet_Users.UserName)nvarchar(256)



    • 但是,当我尝试使用VS 2010设计器设置它时,它给了我以下错误:

       表'aspnet_Users'中的列与现有主键
      或唯一约束不匹配。

      这是不行的,因为 aspnet_Users.UserName 是不是PK的一部分 aspnet_Users ?我试图改变表格,把它作为PK的一部分(我认为这使它成为一个复合键?),但它告诉我要先删除关系,然后才能做到这一点。由于我不知道默认会员表中定义了什么关系,所以我宁愿在走这条路线之前找出更多的关系。 解决方案

错误消息是完全描述的情况 - 你试图在外键约束内使用非关键字段。您建议尝试创建一个组合键来解决这个问题,这将是可行的,但为什么不简单地使用现有的主键?

UserID字段是一个唯一标识符PK,所以它已经被配置为在FK中使用。而且,你会利用规范化,所以不是每个用户在每行存储(最多)256字节,而只是存储uniqueidentifier(16字节)。

I'd like to setup a foreign key to a default membership table but I'm getting an error when trying to define it. I'm using the default aspnet_Users table and also my own Posts table. I'm trying to setup the tables as follows:

aspnet_Users

  • UserId (PK) uniqueidentifier
  • UserName nvarchar(256)

Posts

  • PostID (PK) int
  • UserName (FK -> aspnet_Users.UserName) nvarchar(256)

However, when I try to set this up using the VS 2010 designer, it gives me the following error:

The columns in table 'aspnet_Users' do not match an existing primary key
or unique constraint.

Is this not working because aspnet_Users.UserName isn't a part of the PK for aspnet_Users? I've tried to change the table to include that as part of the PK (I think that makes it a composite key?) but it's telling me to delete the relationships first before I can do it. Being as I don't know what relationships the default membership tables define, I'd rather find out more before going that route.

解决方案

The error message is fully describing the situation - you're trying to use a non-key field inside a foreign key constraint. You suggest trying to create a composite key to get around this issue, which would work, but why wouldn't you simply use the existing primary key?

The UserID field is a uniqueidentifier PK, so it is already configured for use in a FK. And, you'd be utilizing normalization, so that rather than storing (up to) 256 bytes per Username on each row, you'd only be storing the uniqueidentifier (16 bytes).

这篇关于ASP.NET成员资格提供程序:定义一个到aspnet_Users.UserName的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 08:23