簡體   English   中英

ASP.Net Identity不創建用戶

[英]ASP.Net Identity not creating user

我現在已經使用ASP.Net Identity幾次。 在一個新項目中,我似乎在創建用戶時遇到問題。

調用_userManager.Create() ,出現以下錯誤。

  The string '{ Name: IX_UserId, Order: 0 }' was not 
  in the expected format to be deserialized by the 
  IndexAnnotationSerializer. Serialized values are expected to have 
  the format '{ Name: 'MyIndex', Order: 7, IsClustered: True, 
               sUnique: False } { } { Name: 'MyOtherIndex' }'.

我試過使用下面的DbContext,除了類名之外,它與我在另一個項目中擁有的DbContext相同,可以正常工作

public partial class ISIdentityDbContext : IdentityDbContext<IdentityUser>
    {

        public ISIdentityDbContext()
            : base("ISIdentityDbContext")
        { }

        public DbSet<ApplicationUserUserInfoMap> ApplicationUserUserInfoMap { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // asp.net identity - call the tables something else..
            modelBuilder.Entity<IdentityRole>().ToTable("ApplicationRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("ApplicationUserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("ApplicationUserLogins");
            modelBuilder.Entity<IdentityUserRole>().ToTable("ApplicationUserRoles");
            modelBuilder.Entity<IdentityUser>().ToTable("ApplicationUser");  
        }              

    }

我嘗試了以下方法:

 using (ISIdentityDbContext context = new ISIdentityDbContext())
            {                
                _userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context));
                IdentityUser user = new IdentityUser();
                user.UserName = "darren";
                _userManager.Create(user, "password");
            }

而且,在擴展ApplicationUser(IdentityUser)時,我確實需要工作

using (ISIdentityDbContext context = new ISIdentityDbContext())
            {
                _userManager = new UserManager<LegacyApplicationUser>(new UserStore<LegacyApplicationUser>(context));
                ApplicationUserUserInfoMap map = new ApplicationUserUserInfoMap();
                map.UserGUID = "anIdFromAnotherTable";

                LegacyApplicationUser user = new LegacyApplicationUser();
                user.UserInfoMap = map;
                user.UserName = "darren";
                _userManager.Create(user, "password");
            }

我的LegacyApplicationUser在哪里:

   public class LegacyApplicationUser : IdentityUser
    {
        public virtual ApplicationUserUserInfoMap UserInfoMap { get; set; }
    }

    public class ApplicationUserUserInfoMap
    {
        public int ID { get; set; }
        public string UserGUID { get; set; }
    }

我很沮喪……無論我是重建數據庫來匹配標准的Identity用戶還是使用擴展版本,我都會不斷收到頂部顯示的相同異常。

我可能已經錯過了一些東西,雖然不知道是什么...。

有任何想法嗎?

好吧-我修好了!

我打算刪除此問題,因為事實證明,這是一個非常狹窄的問題。

就是說,我會將它留在這里,以供其他所有人努力讓EF與並非全部通過EF的數據庫配合使用。

在我們的案例中,我們有一個數據庫不會針對它構建EF(這是一個非常老的數據庫)-但是一些新部件將被EF處理。 ASP.Net Identity部分。

事實證明,我的問題實際上與__MigrationHistory表有關。

DbInterceptor添加到DbContext我可以看到導致錯誤的實際SQL。

我刪除了_MigrationHistory表中的條目,並且一切正常。

我曾經有過同樣的問題

我只是創建沒有密碼的用戶,然后使用密碼哈希器將用戶退回並重新存儲,以解決此問題。 僅當我設置用戶名和密碼時,它才會失敗-我在為數據庫播種的代碼中遇到了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM