上下文错误 - 实体类型ApplicationUser不是当前上下文的模型的一部分

时间:2015-01-06 11:18:14

标签: asp.net-mvc-5 entity-framework-6 asp.net-identity owin

好吧,我在没有任何帮助的情况下尝试过很多东西。 情况是我将连接字符串作为常规连接字符串,例如:

<add name="MyConnectionString" connectionString="Data Source=mydatasource;Connection Lifetime=10;Max Pool Size=800;Initial Catalog=mydb;Persist Security Info=True;MultipleActiveResultSets=True;User ID=myuser;Password=mypass" providerName="System.Data.SqlClient" />

我有这个错误:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

错误来自:

public partial class myConnectionString : DbContext
{
    public myConnectionString()
        : base("name=myConnectionString")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();// error comes from here
    }

    public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
    public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
    public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
    public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
    public virtual DbSet<ConnectedUsers> ConnectedUsers { get; set; }
}

如果我有EF连接字符串。例如:

    <add name="myConnectionString" connectionString="metadata=res://*/HdkDb.csdl|res://*/HdkDb.ssdl|res://*/HdkDb.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mydatasource;initial catalog=mydb;user id=myuser;password=mypass;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

我有这个错误:

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

错误来自:

 var user = UserManager.Find(email, password);

这是我在startup.auth.cs

中的内容
public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

需要注意的事项: 我在一个项目中有Entity框架(EDMX),在另一个项目中有Authentication(IdentityModel,Usermanager)。我从另一个MVC 5项目(我有startup.auth.cs)中使用它。 在所有项目app / web config中,我都有相同的连接字符串。

我该如何解决这个问题?任何帮助!!

0 个答案:

没有答案