使用SQL Server Compact与ASP.NET Identity

5

我正在使用最新的ASP.NET MVC、Identity和Entity Framework。

示例Identity项目使用Sql Server Express。我想使用Sql Server Compact。

我的做法:

  • Created a new empty solution (not MVC template)

  • Installed sample project: Install-Package Microsoft.AspNet.Identity.Samples -Pre

  • Installed: Install-Package Microsoft.SqlServer.Compact

  • Installed: Install-Package EntityFramework.SqlServerCompact

  • Changed default connection string in web.config to:

    <add name="Foo" 
         providerName="System.Data.SqlServerCe.4.0" 
         connectionString="Data Source=|DataDirectory|\Foo.sdf;" />
    
  • Changed ApplicationDbContext to use the Foo connection string:

    ApplicationDbContext() : base("name=Foo", false)
    

问题在于,在第一次访问ApplicationDbContext时(在种子数据中,在ApplicationDbInitializer.InitializeIdentityForEF()方法中):

  • 数据库将被创建。
  • 对于行result = userManager.SetLockoutEnabled(user.Id, false);,我会收到一个InvalidOperationException: UserId not found

我原以为只需要更改连接字符串即可解决问题——那么我做错了什么?换句话说,我该如何将解决方案从SQL Server Express转换为SQL Server Compact?


1
我已经按照您的指示操作,一切都正常工作。 - Xavier Egea
1个回答

4
发现错误了。我的连接字符串有误,我写成了:
<add name="Foo"
     providerName="System.Data.SqlServerCe.4.0"
     connectionString="
       Data Source=|DataDirectory|\Foo.sdf;
       Persist Security Info=false;
       "/>

以下代码可行(因为 XML 标记中的空格会被忽略,但不是在所有情况下都如此):

<add name="Foo"
     providerName="System.Data.SqlServerCe.4.0"
     connectionString="Data Source=|DataDirectory|\Foo.sdf;Persist Security Info=false;" />

无论如何,这个问题本身包含了一个使用sqlce和Identity2的逐步说明。也许它可以帮助某些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接