Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# 从IdentityUser继承我在UserManager上收到一个错误<;用户>;_C#_Asp.net_Entity Framework_Asp.net Identity - Fatal编程技术网

C# 从IdentityUser继承我在UserManager上收到一个错误<;用户>;

C# 从IdentityUser继承我在UserManager上收到一个错误<;用户>;,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,我正在用.NET Framework 4.5.1和Asp.NET Identity 2.1.0开发一个Web Api 2.2应用程序 我不确定我在做什么,但我想将我的数据库与ASP.NET Identity数据库合并,我已完成以下操作: 我自己的dbContext public class EFDbContext : IdentityDbContext, IUnitOfWork 我自己的用户类 public class User : IdentityUser<long,

我正在用.NET Framework 4.5.1和Asp.NET Identity 2.1.0开发一个Web Api 2.2应用程序

我不确定我在做什么,但我想将我的数据库与ASP.NET Identity数据库合并,我已完成以下操作:

我自己的
dbContext

public class EFDbContext : IdentityDbContext, IUnitOfWork
我自己的
用户

public class User : 
   IdentityUser<long,
                IdentityUserLogin<long>,
                IdentityUserRole<long>,
                IdentityUserClaim<long>
               >
这里有三个错误:

EFDbContext_ctx = context as EFDbContext;
_userManager = new UserManager<User, long>(new UserStore<User>(_ctx));
EFDbContext\u ctx=context作为EFDbContext;
_userManager=newusermanager(newuserstore(_ctx));
  • “Microsoft.AspNet.Identity.UserManager.UserManager(Microsoft.AspNet.Identity.IUserStore)”的方法重载的最佳匹配具有一些无效参数-
  • “Data.Models.User”类型不能用作“TUser”类型或泛型方法的参数 “Microsoft.AspNet.Identity.EntityFramework.UserStore”。没有从“Data.Models.User”的隐式引用转换为“Microsoft.AspNet.Identity.EntityFramework.IdentityUser”
  • 参数1:无法从“Microsoft.AspNet.Identity.EntityFramework.UserStore”转换为“Microsoft.AspNet.Identity.IUserStore”
  • 如何修复此新错误?

    使用

    UserManager\u UserManager;
    
    您使用的是:

    表示用户的主键为字符串类型的用户的用户管理器


    当您使用自己的用户类进行扩展时,您必须提供Identity使用的所有类型

    这是我的上下文类,请注意,我使用字符串作为键:

    public class EarlyDetectionContext  
       : IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim>
    
    public类早期检测上下文
    :IdentityDbContext
    
    当然,您还需要为角色userlogin、userclaim和userroles创建类。 这是我的:

    public class EDClaim : IdentityUserClaim<string>
    {
    }  
    public class EDLogin : IdentityUserLogin<string>
    {
    }
    [Table("Roles", Schema = "ED")]
    public class Role : IdentityRole<string, RoleUserAssociation>
    {
        [Required, Column("DisplayName")]
        public string DisplayName { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    }  
    [Table("RoleUserAssociations", Schema = "ED")]
    public class RoleUserAssociation : IdentityUserRole<string>
    {
        //
        //  Due to Identity 2 the Id needs to of string type
        //
        [Key]
        [Required]
        public string ID { get; set; }
    
        //[Required, Column("User_Id")]
        //public User User { get; set; }
    
        //[Required, Column("Role_Id")]
        //public Role Role { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    }  
    [Table("Users", Schema = "ED")]
    public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim>
    {
        // This is needed for the new Identity 2 framework
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    
        [Required, Column("Municipality_Id")]
        public Municipality Municipality { get; set; }
    
        public string PasswordSalt { get; set; }
    
        [Required, Column("FirstName")]
        public string FirstName { get; set; }
    
        [Required, Column("LastName")]
        public string LastName { get; set; }
    
        [Column("Title")]
        public string Title { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    
        public bool Complete { get; set; }
    
        [Required]
        public DateTime Activated { get; set; }
    }
    
    公共类EDClaim:IdentityUserClaim
    {
    }  
    公共类EDLogin:IdentityUserLogin
    {
    }
    [表(“角色”,Schema=“ED”)]
    公共类角色:IdentityRole
    {
    [必填,列(“显示名称”)]
    公共字符串DisplayName{get;set;}
    [必填,列(“已创建”)]
    已创建公共日期时间{get;set;}
    [必填,第列(“更新”)]
    公共日期时间已更新{get;set;}
    [必填,第列(“已删除”)]
    公共bool已删除{get;set;}
    }  
    [表(“RoleUserAssociations”,Schema=“ED”)]
    公共类角色关联:IdentityUserRole
    {
    //
    //由于标识2,Id需要为字符串类型
    //
    [关键]
    [必需]
    公共字符串ID{get;set;}
    //[必填,第列(“用户Id”)]
    //公共用户{get;set;}
    //[必填,列(“角色Id”)]
    //公共角色{get;set;}
    [必填,列(“已创建”)]
    已创建公共日期时间{get;set;}
    [必填,第列(“更新”)]
    公共日期时间已更新{get;set;}
    [必填,第列(“已删除”)]
    公共bool已删除{get;set;}
    }  
    [表(“用户”,Schema=“ED”)]
    公共类用户:IdentityUser
    {
    //这是新的Identity 2框架所需要的
    公共异步任务GenerateUserIdentityAsync(用户管理器)
    {
    //注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
    var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
    //在此处添加自定义用户声明
    返回用户身份;
    }
    [必填,第列(“市镇Id”)]
    公共自治市{get;set;}
    公共字符串密码salt{get;set;}
    [必填,第列(“名字”)]
    公共字符串名{get;set;}
    [必填,第列(“姓氏”)]
    公共字符串LastName{get;set;}
    [栏目(“标题”)]
    公共字符串标题{get;set;}
    [必填,列(“已创建”)]
    已创建公共日期时间{get;set;}
    [必填,第列(“更新”)]
    公共日期时间已更新{get;set;}
    [必填,第列(“已删除”)]
    公共bool已删除{get;set;}
    公共布尔完成{get;set;}
    [必需]
    已激活公共日期时间{get;set;}
    }
    
    您需要在每个类上指定键类型,在我的例子中是字符串,在您的例子中是long。 还要注意,我的claim和login类是空的。没关系,我不需要在我的案例中添加任何功能或属性,但我需要指定它们

    并且需要用户类中的GenerateUserIdentityAsync方法来实现

    在Startup.Auth.cs文件中,您需要添加:

    // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(EarlyDetectionContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);  
    
    //将数据库上下文、用户管理器和登录管理器配置为每个请求使用一个实例
    app.CreatePerOwinContext(EarlyDetectionContext.Create);
    app.CreatePerOwinContext
    它基本上描述了你要做的事情。(但采用db优先的方法)
    也要看他的(他英语说得很好)。
    而且它也是


    我希望这能为您提供足够的信息,以便继续前进:)

    谢谢您的回答。现在,我在这里遇到了同样的问题:
    \u userManager=newusermanager(newuserstore(_ctx))。您的用户存储应该将您的上下文作为其类型参数。我有一些类似的东西。您是否通过实例化
    UserManager
    修复了最后一个错误?请看我的问题
    public class EDClaim : IdentityUserClaim<string>
    {
    }  
    public class EDLogin : IdentityUserLogin<string>
    {
    }
    [Table("Roles", Schema = "ED")]
    public class Role : IdentityRole<string, RoleUserAssociation>
    {
        [Required, Column("DisplayName")]
        public string DisplayName { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    }  
    [Table("RoleUserAssociations", Schema = "ED")]
    public class RoleUserAssociation : IdentityUserRole<string>
    {
        //
        //  Due to Identity 2 the Id needs to of string type
        //
        [Key]
        [Required]
        public string ID { get; set; }
    
        //[Required, Column("User_Id")]
        //public User User { get; set; }
    
        //[Required, Column("Role_Id")]
        //public Role Role { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    }  
    [Table("Users", Schema = "ED")]
    public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim>
    {
        // This is needed for the new Identity 2 framework
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    
        [Required, Column("Municipality_Id")]
        public Municipality Municipality { get; set; }
    
        public string PasswordSalt { get; set; }
    
        [Required, Column("FirstName")]
        public string FirstName { get; set; }
    
        [Required, Column("LastName")]
        public string LastName { get; set; }
    
        [Column("Title")]
        public string Title { get; set; }
    
        [Required, Column("Created")]
        public DateTime Created { get; set; }
    
        [Required, Column("Updated")]
        public DateTime Updated { get; set; }
    
        [Required, Column("Deleted")]
        public bool Deleted { get; set; }
    
        public bool Complete { get; set; }
    
        [Required]
        public DateTime Activated { get; set; }
    }
    
    // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(EarlyDetectionContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);