The entity type ApplicationUser is not part of the model for the current context. Used two different databases at beginning of project

后端 未结 2 611
无人及你
无人及你 2021-01-14 16:03

I\'ve created an MVC 4 application using entity framework to read and write data to a database I am hosting on an Azure database. The Azure database was supposed to keep th

相关标签:
2条回答
  • 2021-01-14 16:49

    Use your database name instead of passing connection string. Check the database name in my example TestDB

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("TestDB", throwIfV1Schema: false)
            {
            }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }
    
    0 讨论(0)
  • 2021-01-14 16:50

    I have tried to reproduce your issue according with below steps:

    1) create Asp.net MVC template, then register a new user.

    Result: We could find user info on local database.

    2) Add controller with views using Entity Framework. And use Azure SQL database as its resource.

    Result: we will find two connection in our web.config

    3) Delete default connection string

    4) Change Application DB Context connection string

       <add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("jambdbEntities", throwIfV1Schema: false)
            {
            }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }
    

    After above steps, My application give me below error:

    Solution:

    1) Edit 'DefaultConnection' connection string

      <connectionStrings>
        <add name="jambdbEntitiesapplication"   providerName="System.Data.SqlClient" connectionString="Server=tcp:jambdb.database.windows.net,1433;Initial Catalog=jambdb;Persist Security Info=False;User ID=jambor;Password=***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
        <add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    

    2) Modify the code:

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("jambdbEntitiesapplication", throwIfV1Schema: false)
            {
            }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }
    

    3) Modify AutomaticMigrationsEnabled = true; in Configuration class under Migrations folder.

    Here is the result:

    0 讨论(0)
提交回复
热议问题