【问题标题】:UserStore with my own databaseUserStore 和我自己的数据库
【发布时间】:2014-01-27 22:39:27
【问题描述】:

我正在尝试使用我自己的 SQL Server 数据库来存储用户权限。

我用这样的上下文初始化 UserStore(我也尝试使用默认构造函数)

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

然后我创建了一个空数据库并为实体生成了数据模型。

然后我更改了连接的网络配置

<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>

但它不起作用。如果我尝试注册用户,在

IdentityResult result = await UserManager.CreateAsync(user);

它会调用

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

    base.Dispose(disposing);
}

我尝试使用已创建表的数据库,但结果相同。

我尝试更改DefaultConnection 的连接名称,但没有成功。

我尝试了关于 question 的建议,并将 DbContext 更改为 IdentityDbContext&lt;IdentityUser&gt;

public partial class DbEpicerieEntities : IdentityDbContext<IdentityUser>
    {
        public DbEpicerieEntities()
            : base("DefaultConnection")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
    }

但我有同样的问题。

我不知道网络配置中的 EntityFramework 设置是否可能是问题

<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>

任何想法为什么它不起作用?

谢谢

【问题讨论】:

    标签: c# asp.net sql-server entity-framework asp.net-web-api


    【解决方案1】:

    使用以下连接字符串为我解决了这个问题:

    <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=Epicerie;Integrated Security=True;" providerName="System.Data.SqlClient" />
    

    【讨论】:

      【解决方案2】:

      你能发布你的 MyContext 课程吗?您可以尝试将您的第一个代码 sn-p 更改为使用您的 DbEpicerieEntities 类而不是 MyContext 类。

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

      【讨论】:

      • 抱歉,我的问题有误。我做了编辑。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2011-01-24
      • 2014-09-03
      • 2015-09-13
      • 1970-01-01
      • 2012-10-12
      相关资源
      最近更新 更多