Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户管理器<;ApplicationUser>;。Create()返回null_C#_Asp.net_Asp.net Mvc_Asp.net Identity - Fatal编程技术网

C# 用户管理器<;ApplicationUser>;。Create()返回null

C# 用户管理器<;ApplicationUser>;。Create()返回null,c#,asp.net,asp.net-mvc,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Identity,我在Configuration.cs文件的Seed方法中有以下代码: var userStore = new UserStore<ApplicationUser>(); var manager = new UserManager<ApplicationUser>(userStore); IdentityResult result = manager.Create(new ApplicationUser() { UserName = "test@mail.com", Em

我在Configuration.cs文件的Seed方法中有以下代码:

var userStore = new UserStore<ApplicationUser>();
var manager = new UserManager<ApplicationUser>(userStore);

IdentityResult result = manager.Create(new ApplicationUser() { UserName = "test@mail.com", Email = "test@mail.com", Name = "Martin Tracey" }, "password");
if (result.Succeeded) { Console.WriteLine("User created successfully"); }
else {
    Console.WriteLine("Something went wrong. result is "+result.ToString());
    foreach (var error in result.Errors) Console.WriteLine(error); 
}
var userStore=new userStore();
var-manager=新的UserManager(userStore);
IdentityResult result=manager.Create(新应用程序用户(){UserName=”test@mail.com“,电子邮件=”test@mail.com,Name=“Martin Tracey”},“密码”);
if(result.successed){Console.WriteLine(“用户创建成功”);}
否则{
WriteLine(“出现了问题。结果是”+result.ToString());
foreach(result.Errors中的var error)Console.WriteLine(error);
}
无论出于何种原因,
manager.Create
调用返回
null


知道这个方法为什么会返回null吗?

我知道了!这是一个非常简单的解决方案

My
userStore
变量没有允许其访问和写入数据库的DbContext。简单的解决方案是使用传递到Seed方法中的上下文。现在真是魅力四射!见下文:

protected override void Seed(MyFirstWebApplication.Models.ApplicationDbContext context)
{
    if( !context.Users.Any( u => u.Email == "test@mail.com" ) )
    {
        var userStore = new UserStore<ApplicationUser>(context);
        var manager = new UserManager<ApplicationUser>(userStore);
        var user = new ApplicationUser() { UserName = "test@mail.com", Email = "test@mail.com", Name = "Martin Tracey" };
        IdentityResult result = manager.Create(user, "password");}
    }
}
protected override void Seed(MyFirstWebApplication.Models.ApplicationDbContext上下文)
{
如果(!context.Users.Any)(u=>u.Email==)test@mail.com" ) )
{
var userStore=新的userStore(上下文);
var-manager=新的UserManager(userStore);
var user=new ApplicationUser(){UserName=”test@mail.com“,电子邮件=”test@mail.com,Name=“Martin Tracey”};
IdentityResult result=manager.Create(用户,“密码”);}
}
}