code第一隶属提供商 [英] code first membership provider
问题描述
我怎么能先用code集成EF 5.0的成员资格提供?
How can I integrate EF 5.0 with membership provider using code first?
我有我想要使用用户等注册自己的数据库架构。
I have my own database schema which I want to use for registration of users etc.
推荐答案
您应该看一看在<一个href=\"http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx\">SimpleMembershipProvider
这是很容易与EF一起使用。
It is very easy to use it together with EF.
更新
有关MVC4我会先从空白模板。
For MVC4 I would start with the blank template.
您需要 WebSecurity.InitializeDatabaseConnection
来设置数据库。
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "UserName", true);
这需要作为参数一ConnectionString,则表,一个唯一的标识符列和用户名栏的名称。
It takes as parameters a name of a connectionstring, the table, a unique identifier column and the username-column.
我用这块$ C $上面C的模式是:
The model I use for this piece of code above is:
[Table("Users")]
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
要登录某人:
WebSecurity.Login(aUsername, aPassword);
上面的方法返回一个布尔值,如果这是真的,登录为全成。
The method above returns a bool, if it is true, login was successfull.
在Web.config您不要需要定义的MembershipProvider,因为它是默认ASP.NET成员资格。
In the Web.config you do not need to define a membershipProvider as it was with default ASP.NET Membership.
如果您需要获得对供应商接入(删除账户):
If you need to gain access to the provider (to delete an account):
var provider = (SimpleMembershipProvider) Membership.Provider;
provider.DeleteAccount(aUsername); // Delete the account
provider.DeleteUser(aUsername, true); // delete the user and its data
有关创建新用户(在这种情况下,我给出的模型)
For creating a new user (with my given model in this case)
WebSecurity.CreateUserAndAccount(aUsername, aPassword, new { Email = aEmail, FirstName = aFirstName, LastName = aLastName });
好处是,当您希望与正常asp.net成员要做到这一点,你现在可以使用你的其他类EF作为外键模型,而无需麻烦。 : - )
Benefit is, that you can now use your model for other EF classes as a foreign key without having the hassle when you want to do this with the normal asp.net membership. :-)
这篇关于code第一隶属提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!