我在一个单独的库中(例如Common.Feedback.Data
)有一个基于数据库的EDMX模型,其中包括AspNetUser
表及其相关的Identity Framework表(从另一个现有的、可工作的数据库/应用程序中提取)。
我已经更新了ApplicationDbContext
连接字符串,使其指向新的模型和新的数据库连接:
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Feedback.MvcApplication.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("FeedbackEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
在 web.config 中的连接字符串包括了程序集的完整路径,该引用是正确的:
<add name="FeedbackEntities"
connectionString="metadata=res://Common.Feedback.Data/FeedbackModel.csdl|res://Common.Feedback.Data/FeedbackModel.ssdl|res://Common.Feedback.Data/FeedbackModel.msl;provider=System.Data.SqlClient;provider connection string="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
运行时,任何访问登录页面的操作都会导致以下错误:
"The entity type ApplicationUser is not part of the model for the current context"
我尝试了所有与该错误相关的链接,通常涉及更新迁移的方式。
由于这是一个EDMX首次设置,我无法启用迁移。但是,我认为ApplicationUser
对象和aspnetusers
表之间“某处”有绑定关系。
这里是否有人清楚地了解如何在运行时将ApplicationUser
类映射到aspnetusers
表,并解释如何使我的代码与数据库一起工作?
复制步骤
- 使用VS 2013创建新的MVC Web应用程序
- 将所有NuGet包更新到最新版本
- 拷贝现有的带Identity Framework表的SQL数据库到新表(删除任何不相关的表)
- 添加项目的新表
- 创建一个类库来保存数据模型(例如:
Common.Feedback.Data
) - 基于先前创建的数据库,在库中添加Edmx数据模型
- 更改连接字符串以完全限定程序集(不使用
res://*/
) - 在
IdentityModel.cs
中更改连接字符串名称,以匹配配置文件中的连接字符串名称。 - 从库的
app.config
复制连接字符串到Web项目的web.config
中 - 尝试登录,您将遇到上述错误
更新:
根据一个杂散的帖子,我将我的连接字符串更改为与默认情况下Identity Framework配置使用的普通SQL连接匹配(并使用Sql客户端):
<add name="FeedbackSql"
connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
并且修改设置以使用这个新连接:
public ApplicationDbContext()
: base("FeedbackSql", throwIfV1Schema: false)
{
}
出现了奇怪的错误:
GZip头中的魔术数字不正确。确保您正在传入GZip流。
我认为更改为 SqlClient 提供程序是正确的,所以这个新错误可能与在该连接上使用 Azure 数据库有关。我愿意尝试下一步要尝试什么。
根据 @rism 的建议和 这个链接 更新了 web.config(但 GZip 错误仍然存在):
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<!--<parameter value="mssqllocaldb" />-->
<parameter value="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=myuserid;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
再次根据 @rism 提供的提示,我尝试了这个版本(但 GZip 错误仍然存在):
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
最新更新:
我创建了一个全新的Web应用程序,带有标准的用户安全选项。我还在Azure中创建了一个空数据库。
我仅仅改变了默认连接字符串到这个:
<connectionStrings>
<add name="DefaultConnection"
connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>
并将默认连接工厂设置为:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
登录时出现以下错误:引号块: GZip头部的魔数不正确。请确保传入的是一个GZip流。 描述:在当前web请求的执行过程中发生了未处理的异常,请检查堆栈跟踪以获取有关错误及其来源代码的更多信息。
异常详细信息:System.IO.InvalidDataException:GZip头部中的魔数不正确,请确保传入的是GZip流。
源错误:
Line 153: { Line 154: var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; Line 155: var result = await UserManager.CreateAsync(user, model.Password); Line 156: if (result.Succeeded) Line 157: { }
ApplicationDbContext
。您可以删除所有多余的内容,拥有自己的 DbContext 并避免麻烦。 - jamesSampica