我對UserManager.Create(...)
有點困惑。Asp.Net身份不會在使用自定義用戶時創建數據庫
當我使用標準EntityFramework的IdentityUser
來創建和添加一個新的用戶一切正常。如果我的連接字符串Data Source=(LocalDb)\MSSQLLocalDB;Database=IdentityDemo;trusted_connection=yes;
不存在,EntityFramework將創建一個數據庫,並向數據庫添加一個用戶。
ExampleProgram:
static void Main(string[] args)
{
var username = "ygg";
var password = "Password123!";
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var creationResult = userManager.Create(new IdentityUser(username), password);
}
但是當我嘗試用我自己的CustomUser,的EntityFramework什麼都不做,我也得到在UserManager.Create(...)
一個NullReferenceException。
這裏是我的擴展CustomUser項目:
static void Main(string[] args)
{
var userName = "Ygg";
var password = "Password123!";
var userStore = new CustomUserStore(new CustomUserDbContext());
var userManager = new UserManager<CustomUser, int>(userStore);
var creationResult = userManager.Create(
new CustomUser {UserName = userName}, password); //<--- Exception Here
}
CustomUser級:
public class CustomUser : IUser<int>
{
public int Id { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
}
CustomUserDbContext:
public class CustomUserDbContext : IdentityDbContext
{
public CustomUserDbContext() : base("DefaultConnection")
{
}
public DbSet<CustomUser> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var user = modelBuilder.Entity<CustomUser>();
user.ToTable("Users");
user.HasKey(x => x.Id);
user.Property(x => x.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
user.Property(x => x.UserName).IsRequired().HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true }));
base.OnModelCreating(modelBuilder);
}
}
最後但並非最不重要的,CustomUserStore:
public class CustomUserStore : IUserPasswordStore<CustomUser, int>
{
private readonly CustomUserDbContext _context;
public CustomUserStore(CustomUserDbContext context)
{
_context = context;
}
public void Dispose()
{
_context.Dispose();
}
public Task CreateAsync(CustomUser user)
{
_context.Users.Add(user);
return _context.SaveChangesAsync();
}
.
.
.
}
例外:
對象引用不設置爲一個對象的一個實例
堆棧跟蹤:
貝Microsoft.AspNet.Identity.UserManager
2.<UpdatePassword>d__39.MoveNext() bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei Microsoft.AspNet.Identity.UserManager
2.d__d.MoveNext () bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) bei System.Runtime.CompilerServices.TaskAwaiter.Handl eNonSuccessAndDebuggerNotification(任務任務) bei Microsoft.AspNet.Identity.AsyncHelper.RunSync [函數](Func1 func) bei Microsoft.AspNet.Identity.UserManagerExtensions.Create[TUser,TKey](UserManager
2 manager,TUser user,String password) bei Identity.Program.Main(String [] args)in E:\ syncplicity \ z003apfp \ Documents \ Visual Studio 2015 \ Projects \ Identity \ Identity \ Program.cs:Zeile 21. bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) bei System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity ,字串[] args)
我使用的EntityFramework 6.1.0和2.2.1 AspNet.Identity.Core
我錯過了什麼配置,所以AspNet.Identity和EntityFrameworks在這裏接受我自己的實現?
任何問題所在的幫助或想法,讚賞。
我已經嘗試先建立數據庫,然後嘗試添加使用userManager.Create(new CustomUser {UserName = userName}, password)
用戶,而且當你重寫你應該做它身份的整體結構,這導致相同的NullReferenceException
發佈整個NullRef異常。 – mxmissile
我有點困惑。你想讓ASP.NET Identity爲你創建'Users'表,並使用該表代替'AspNetUsers'表嗎? – Win
是的,那正是我想要完成的。 – Yggraz