我有一个MVC 5应用程序。我想使用标准的aspnet.identity提供程序来启用用户通过标准\Account视图登录。然而,我还需要连接到第三方数据库,并希望首先使用EF代码。
我的主数据库的上下文取自Scott Allen Pluralsight。我不知道"ApplicationUser"类继承IdentityUser的原因或内容,但它在他的视频和我之前创建的网站中都起了作用。它看起来如下:
namespace ESBAMPortal.DataLayer
{
// 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
{
}
class PortalContext : IdentityDbContext<ApplicationUser>, ESBAMPortal.DataLayer.IPortalContext
{
public PortalContext()
: base("ESBamPortalConString")
{
this.Configuration.LazyLoadingEnabled = false;
}
public virtual DbSet<DomainClasses.Portal> Portal {get; set;}
}
}
第三方数据库的上下文如下:
namespace ESBAMPortal.DataLayer
{
public class ESBContext : DbContext, ESBAMPortal.DataLayer.IESBContext
{
public ESBContext()
: base("ESBExceptionConString")
{
this.Configuration.LazyLoadingEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public virtual DbSet<ActionType> ActionTypes { get; set; }
public virtual DbSet<Alert> Alerts { get; set; }
public virtual DbSet<AlertCondition> AlertConditions { get; set; }
public virtual DbSet<AlertEmail> AlertEmails { get; set; }
public virtual DbSet<AlertHistory> AlertHistories { get; set; }
public virtual DbSet<AlertSubscription> AlertSubscriptions { get; set; }
public virtual DbSet<AlertSubscriptionHistory> AlertSubscriptionHistories { get; set; }
public virtual DbSet<AuditLog> AuditLogs { get; set; }
public virtual DbSet<AuditLogMessageData> AuditLogMessageDatas { get; set; }
public virtual DbSet<Batch> Batches { get; set; }
public virtual DbSet<Configuration> Configurations { get; set; }
public virtual DbSet<ContextProperty> ContextProperties { get; set; }
public virtual DbSet<Fault> Faults { get; set; }
public virtual DbSet<Message> Messages { get; set; }
public virtual DbSet<MessageData> MessageDatas { get; set; }
public virtual DbSet<ProcessedFault> ProcessedFaults { get; set; }
public virtual DbSet<UserSetting> UserSettings { get; set; }
}
}
当我运行网站时,我可以从第三方网站提取数据。然而,当我尝试登录时,我收到消息:
Invalid column name 'Discriminator'
这是由于进行调用而抛出的:
var user = await UserManager.FindAsync(model.UserName, model.Password);
运行EF Profiler我可以看到它正在尝试执行以下SQL:
SELECT [Limit1].[C1] AS [C1],
[Limit1].[Id] AS [Id],
[Limit1].[UserName] AS [UserName],
[Limit1].[PasswordHash] AS [PasswordHash],
[Limit1].[SecurityStamp] AS [SecurityStamp]
FROM (SELECT TOP (1) [Extent1].[Id] AS [Id],
[Extent1].[UserName] AS [UserName],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
'0X0X' AS [C1]
FROM [dbo].[AspNetUsers] AS [Extent1]
WHERE ([Extent1].[Discriminator] = N'ApplicationUser')
AND ((((UPPER([Extent1].[UserName])) = (UPPER('rob' /* @p__linq__0 */)))
AND (NOT ((UPPER([Extent1].[UserName]) IS NULL)
OR (UPPER('rob' /* @p__linq__0 */) IS NULL))))
OR ((UPPER([Extent1].[UserName]) IS NULL)
AND (UPPER('rob' /* @p__linq__0 */) IS NULL)))) AS [Limit1]
问题是,AspNetUsersTable没有鉴别器列。
有人能解释一下我哪里出了问题,我该怎么解决吗?
我回到了基础,创建了一个新的MVC 5应用程序,然后通过从包管理器控制台运行以下命令生成了数据库迁移:
- 启用迁移
- 先添加迁移
当我检查生成的代码时,我可以看到它在AspNetUsers表中包含了一个"Discriminator"列。我回到我的"真实"解决方案,比较了我单独的DataAccess项目中的"第一个"迁移代码。这段代码没有"Discriminator"列,我还注意到它有额外的列,如"PhoneNumber"one_answers"TwoFactorEnabled"。这让我觉得MVC5模板可能使用了以前版本的"Microsoft.AspNet.Identity.EntityFramework"——事实证明是这样。
为了解决这个问题,我转到MVC5 web应用程序的管理Nuget软件包对话框,卸载了"Microsoft.AspNet.Identity.EntityFramework"(v1.0),然后重新安装(v2.0)。随后,当我从网站运行登录时,它没有试图找到"鉴别器"列,一切都很好。
我想这给我上了一堂宝贵的课,让我更加关注VS模板
因为ASP.NET MVC项目有一个"ApplicationUser"类已经覆盖了IdentityUser,所以它会自动将Discriminator列添加到实体框架中。请尝试删除数据库并重新运行。再一次
检查这个http://forums.asp.net/t/1970682.aspx?ASP+NET+标识+V2+鉴别器+列
当我添加从IdentityDbContext继承的两个类时,如果是这样,请将鉴别器添加到AspNetRoles
AddColumn("dbo.AspNetRoles", "Discriminator", c => c.String(nullable: false, maxLength: 128));
尝试删除数据库并重新运行。再次
检查此问题1.http://forums.asp.net/t/1970682.aspx?ASP+NET+标识+V2+鉴别器+列。http://
另请查看此博客msdn.com/b/webdev/archive/2013/10/16/customizing-file-information-in-asp-net-identity-in-vs-2013-templates.aspx