ASP.NET 成员资格提供程序:定义 aspnet_Users.UserName 的外键
我想为默认成员资格表设置外键,但在尝试定义它时遇到错误。我使用默认的 aspnet_Users 表和我自己的 Posts 表。我尝试按如下方式设置表:
aspnet_Users
- UserId (PK) uniqueidentifier
- UserName nvarchar(256)
Posts
- PostID (PK) int
- UserName (FK -> aspnet_Users.UserName) nvarchar(256)
但是,当我尝试设置此值时使用 VS 2010 设计器启动时,它给我以下错误:
The columns in table 'aspnet_Users' do not match an existing primary key
or unique constraint.
这不起作用,因为 aspnet_Users.UserName
不是 aspnet_Users
PK 的一部分吗?我尝试更改表以将其作为 PK 的一部分包含在内(我认为这使其成为复合键?),但它告诉我先删除关系,然后才能执行此操作。由于我不知道默认成员资格表定义了哪些关系,因此我宁愿在走这条路线之前了解更多信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息完全描述了情况 - 您试图在外键约束内使用非键字段。您建议尝试创建一个组合键来解决此问题,这可行,但为什么不简单地使用现有的主键呢?
UserID 字段是唯一标识符 PK,因此它已配置为在 FK 中使用。而且,您将利用标准化,这样您就只需存储唯一标识符(16 个字节),而不是在每行上为每个用户名存储(最多)256 个字节。
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).
我认为您还需要将其与 ApplicationID 结合起来。 UserID 不唯一,除非与应用程序 ID 结合使用。
I think you need to combine it with ApplicationID as well. UserID is not unique except when combined with the application ID.