UserStore with my own database

2019-09-10 13:59发布

I'm trying to use my own SQL Server database to store the user permissions.

I initialize the UserStore with my context like this (I tried with the default constructor also)

UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new DbEpicerieEntities()));

Then I created an empty database and generated the data model for entity.

Then I changed the web config for my connection

<connectionStrings>
    <add name="DefaultConnection" connectionString="metadata=res://*/Models.EpicerieModel.csdl|res://*/Models.EpicerieModel.ssdl|res://*/Models.EpicerieModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=DbEpicerie;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

But it doesn't work. If I try to register a user, after

IdentityResult result = await UserManager.CreateAsync(user);

it will call

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
       UserManager.Dispose();
    }

    base.Dispose(disposing);
}

I tried to use a database with tables already made, but I have the same result.

I tried to change the connection name for DefaultConnection but no success.

I tried the recommendations on that question and I changed DbContext to IdentityDbContext<IdentityUser>

public partial class DbEpicerieEntities : IdentityDbContext<IdentityUser>
    {
        public DbEpicerieEntities()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

    }

but I have the same problem.

I don't know if the EntityFramework setting in the web config could be the problem

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

Any ideas why it doesn't work?

Thank you

2条回答
Lonely孤独者°
2楼-- · 2019-09-10 14:25

Can you post your MyContext class? An attempt you can make is to change your first code snippet to use your DbEpicerieEntities class instead of the MyContext class.

UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new DbEpicerieEntities()));
查看更多
Anthone
3楼-- · 2019-09-10 14:48

Using the following connection string solved the problem for me :

<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=Epicerie;Integrated Security=True;" providerName="System.Data.SqlClient" />
查看更多
登录 后发表回答