0
我正在尝试为SimpleMembershipProvider
播种一些用户。我在UserProfile表中添加了几列,如手机号码。当我尝试添加用户与手机号码,编译器告诉我:为会员供应商播种用户
The name 'Mobile' does not exist in the current context
这是类:
namespace _DataContext.Migrations {
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using WebMatrix.WebData;
using System.Web.Security;
internal sealed class Configuration : DbMigrationsConfiguration<_DataContext.DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(_DataContext.DataContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)System.Web.Security.Membership.Provider;
if (!roles.RoleExists("Administrator"))
roles.CreateRole("Administrator");
if (membership.GetUser("Username", false) == null)
membership.CreateUserAndAccount("Username", "Pass", false,
new Dictionary<string, object>
{
{ Mobile = "+311122334455" },
});
/*if (!WebSecurity.UserExists("test"))
WebSecurity.CreateUserAndAccount(
"Username",
"password",
new {
Mobile = "+311122334455",
FirstName = "test",
LastName = "test",
LoginCount = 0,
IsActive = true,
});
*/
}
}
}
如果我使用WebSecurity
一切顺利。
我在这里做错了什么?
嗨,有道理,我想我也尝试过这个选项,但不知道。但是,对于websecurity来说,你指的是什么,不能指定确切的提供者? – Yustme 2013-05-13 09:07:46
@Yustme。它只是将你从具体的提供者实现中抽象出来。它的代码还需要'Membership.Provider',但是将它转换为所有提供者的公共基类,而不是'SimpleMembershipProvider'(对IoC和/或DI来说可能更好)。除非你对性能超级担心,否则我会使用WebSecurity。 – 2013-05-13 09:10:43
好的,谢谢! – Yustme 2013-05-13 12:20:32