ASP.NET 成员资格和 Linq to SQL
我想知道在 sql server 中设置 asp.net 成员资格以与 linq to sql 良好配合的最佳方法。
我想使用外键关系的用户名字段,并让 linq to sql 识别 asp 用户属性。
我在获取用户表和其他表之间的正确关系时遇到一些问题。我是否需要将用户名设置为主键才能正常工作?
I am wondering the best way about setting up asp.net membership in sql server to work well with linq to sql.
I'd like to use the username field for the foreign key relationships and have linq to sql recognise the asp user property.
I am having some problems getting relationships right between the users table and my other tables. Do I need to set username to a primary key for this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我这样做时,我只是让 aspnet_Users 表中的
UserId
字段作为外键。当您使用LinqToSql时,您仍然可以根据asp中的用户名进行过滤,如下所示:这是因为aspnet_Users表和您自己的表之间存在外键关系。
When I did it, I just let the
UserId
field in the aspnet_Users table be the foreign key. When you use LinqToSql you can still filter based on the UserName in asp like this:This is because of the foreign key relationship between the aspnet_Users table and your own table.
Users 数据库表上的主键约束与 ASP.NET 成员资格提供程序无关(或很少)。成员资格提供程序是实现 System.Web.Security 中定义的抽象类 MembershipProvider 的类。默认情况下,asp.net 应用程序使用链接到 aspnet_XXX 表的成员资格提供程序,但您可以编写自己的提供程序,该提供程序将使用您自己的“用户”表并通过 linq2sql 访问它。
您所要做的就是创建继承自 System.Web.Security.MembershipProvider 的类,并在 web.config 中对其进行设置,如下所示:
您必须实现的方法使用字符串用户名来识别用户,但它不必是主键你的桌子。它应该是唯一的领域。我认为好主意是使用 INT 作为主键。
The primary key constraint on Users database table has nothing (or little) to do with ASP.NET membership provider. Membership provider is a class implementing abstract class MembershipProvider defined in System.Web.Security. By default asp.net applications use membership provider linked to aspnet_XXX tables, but you can write your own provider that will use your own "Users" table and access it through linq2sql.
Al you have to do is create class inheriting from System.Web.Security.MembershipProvider and set up it in web.config like:
The methods you have to implement use string username to identify users, but it doesn't have to be primary key of your table. It should by just unique field. I think the good idea is to use INT as primary keys.