c# – MVC 5不自动在DB中创建AspNetUser表

前端之家收集整理的这篇文章主要介绍了c# – MVC 5不自动在DB中创建AspNetUser表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是MVC 5的新手,我正在尝试在创建的新数据库上下文中配置Individual User Auth,但ApplicationDBContext没有在我的新数据库中创建AspNetUser表.有人能指出我正确的方向吗?

错误

The entity type ApplicationUser is not part of the model for the current context.

IdentityModel.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyEntities",throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

Web.config文件

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSsqlLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MVC5.WebSite-20150723075149.mdf;Initial Catalog=aspnet-MVC5.WebSite-20150723075149;Integrated Security=True" providerName="System.Data.sqlClient" />
  <add name="MyEntities" connectionString="Metadata=res://*/Models.UserModel.csdl|res://*/Models.UserModel.ssdl|res://*/Models.UserModel.msl;provider=System.Data.sqlClient;provider connection string=&quot;data source=devserver;initial catalog=MyEntities;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

我知道MVC应该自动神奇地创建UserStore和相关表,但显然我在这里完成了一些非常愚蠢的事情.任何帮助深表感谢.

解决方法

好吧,我做的事情很愚蠢.基本上尝试让ApplicationDbContext使用由EF创建的连接.不是正确的方式.为了让表格’自动神奇地’创建.我需要在连接到所需数据库的Web.config中添加一个普通的连接字符串,然后在IdentityModel.cs中设置它

Web.config文件

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSsqlLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MVC.WebSite-20150723075149.mdf;Initial Catalog=aspnet-MVC.WebSite-20150723075149;Integrated Security=True" providerName="System.Data.sqlClient" />
  <add name="MyConnection" connectionString="Data Source=devserver;initial catalog=MyCatalog;user id=user;password=pass;Integrated Security=True" providerName="System.Data.sqlClient" />
 </connectionStrings>

IdentityModel.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyConnection",throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

希望,这有助于某人.感谢所有帮助过的人.

原文链接:https://www.f2er.com/csharp/92193.html

猜你在找的C#相关文章