我正在尝试向我的模型中的UserProfile
类添加新属性
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
public string Email { get; set; } \\this is the new property
public virtual IList<Game> Games { get; set; }
}
我正在尝试将其添加到我的Configurations.cs
文件
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("MafiaContext",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)Membership.Provider;
if (!roles.RoleExists("Administrator"))
{
roles.CreateRole("Administrator");
}
if (membership.GetUser("drew", false) == null)
{
membership.CreateUserAndAccount(
"drew",
"password", false,
new { Email = "myemail@gmail.com" }); \\ this line errors
}
if (!roles.GetRolesForUser("drew").Contains("Administrator"))
{
roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
}
}
但是,我收到错误:
Error 2 Argument 4: cannot convert from 'AnonymousType#1' to 'System.Collections.Generic.IDictionary<string,object>' C:\Code\Mafia\Mafia.EF\Migrations\Configuration.cs 65 21 Mafia.EF
但我什么都没有......我的WebMatrix.WebData
版本是v4.0.30319
有什么想法吗?
答案 0 :(得分:20)
试试这样:
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection(
"MafiaContext",
"UserProfile",
"UserId",
"UserName",
autoCreateTables: true
);
if (!Roles.RoleExists("Administrator"))
{
Roles.CreateRole("Administrator");
}
if (!WebSecurity.UserExists("drew"))
{
WebSecurity.CreateUserAndAccount(
"drew",
"password",
new { Email = "myemail@gmail.com" },
false
);
}
if (!Roles.GetRolesForUser("drew").Contains("Administrator"))
{
Roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
}
}
这是关于您可能会考虑通过的SimpleMembershipProvider的nice tutorial
。
答案 1 :(得分:0)
由于SimpleMembershipProvider.CreateUserAndAccount方法的最后一个参数应为IDictionary<string, Object>
,因此您的代码应重写如下:
membership.CreateUserAndAccount(
"drew",
"password",
new Dictionary<string, object> { new { "Email", "myemail@gmail.com" } }
);