Set ASP.NET Identity ConnectionString property in .NET 4.5.1

前端 未结 2 1265
刺人心
刺人心 2021-02-06 06:59

So basically after finally learning how to change OpenAuth to not use DefaultConnection in .NET 4.5, I\'ve moved on to 4.5.1, rendering those learnings moot. The duties of AuthC

2条回答
  •  一整个雨季
    2021-02-06 07:47

    I have followed the approach you suggested and it worked for me. However, there were few things mostly syntactic and naming issues which happened to be different. I think these differences are due to probably the different versions of Visual Studios we used (rather than .NET - my version is release one with .NET 4.5.1). I continue with a description of my specific solution.

    My goal was to have a single DB context with which I can access both User or Identity related data and also my custom application data. To achieve this I completely deleted class ApplicationDbContext which is automatically created for you when you create a new project.

    Then, I created a new class MyDbContext.

    public class MyDbContext: DbContext
    {
        public MyDbContext() : base("name=DefaultConnection")
        {
    
        }
    
        //
        // These are required for the integrated user membership.
        //
        public virtual DbSet Roles { get; set; }
        public virtual DbSet Users { get; set; }
        public virtual DbSet UserClaims { get; set; }
        public virtual DbSet UserLogins { get; set; }
        public virtual DbSet UserRoles { get; set; }
    
        public DbSet Movies { get; set; }
        public DbSet Orders { get; set; }
        public DbSet Purchases { get; set; }
    }
    

    The fields Roles, Users, UserClaims, UserLogins, UserRoles are as suggested required for the membership management. However, in my case their types have different names (ApplicationUser instead of User, IdentityUserClaim instead of UserClaim and etc.). I suppose that was the reason why Antevirus had the "User could not be found" problem.

    Also, as we see in my case there are 5 such fields rather than 8. Probably, this is due to the different versions of the Visual Studio.

    The last change which I made was in class AccountController and it reflects the use of the new context MyDbContext. Here I passed an instance of MyDbContext instead of ApplicationDbContext

    Before

    public AccountController()
        : this(new UserManager(new UserStore(new ApplicationDbContext())))
    {
    }
    

    After

    public AccountController()
        : this(new UserManager(new UserStore(new MyDbContext())))
    {
    }
    

提交回复
热议问题