简体   繁体   中英

MVC identity with database first Entity Framework Error System.InvalidOperationException

I am using the MVC with Identity.I am using custom Identity from here . It's is working fine for local SQL Server 2012, but when I host the database into server and if I try to register it throws this:

System.InvalidOperationException: The entity type MyUser is not part of the model for the current context.

 //Webconfig

<add name="IdentityEntities" connectionString="metadata=res://*/DAL.IdentityModel.csdl|res://*/DAL.IdentityModel.ssdl|res://*/DAL.IdentityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxxx;initial catalog=Identiydatabase;MultipleActiveResultSets=True;App=EntityFramework&quot;"providerName="System.Data.EntityClient"/>

public class AccountController : Controller
{
    private const string XsrfKey = "XsrfId";
    private MyUserManager _userManager;
    public AccountController()
    {
    }
    public AccountController(MyUserManager userManager)
    {
        UserManager = userManager;
    }
     public MyUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<MyUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
  [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new MyUser
            {
                Email = model.Email
            };
            var result = await UserManager.CreateAsync(user,model.Password);
  }

   //Application DBContext
  public class ApplicationDbContext : IdentityDbContext<MyUser, MyRole, long, MyLogin, MyUserRole, MyClaim>
{
    #region constructors and destructors

    public ApplicationDbContext()
        : base("IdentityEntities")
    {
    }
    public static ApplicationDbContext Create()
    { return new ApplicationDbContext();
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Map Entities to their tables.
        modelBuilder.Entity<MyUser>().ToTable("User");
        modelBuilder.Entity<MyRole>().ToTable("Role");
        modelBuilder.Entity<MyClaim>().ToTable("UserClaim");
        modelBuilder.Entity<MyLogin>().ToTable("UserLogin");
        modelBuilder.Entity<MyUserRole>().ToTable("UserRole");
        // Set AutoIncrement-Properties
        modelBuilder.Entity<MyUser>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<MyClaim>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<MyRole>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        // Override some column mappings that do not match our default
        modelBuilder.Entity<MyUser>().Property(r => r.UserName).HasColumnName("Login");
        modelBuilder.Entity<MyUser>().Property(r => r.PasswordHash).HasColumnName("Password");
    }

    #endregion
}

}

通过服务器名称(或IP)与remplace YourServer一起尝试:

<add name="IdentityEntities" providerName="System.Data.SqlClient" connectionString="Server=YourServer;Database=Identiydatabase;Trusted_Connection=True;MultipleActiveResultSets=true;" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM