我正在将MVC与Identity一起使用。我正在使用此处的自定义身份。它对本地SQL Server 2012的工作正常,但是当我将数据库托管到服务器中时,如果我尝试注册它,则会抛出以下内容:
system.invalidoperationException:实体类型Myuser不是当前上下文的模型的一部分。
//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="data source=xxxxx;initial catalog=Identiydatabase;MultipleActiveResultSets=True;App=EntityFramework""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
}
}
尝试使用remplace YourServer
by Server name(或IP(:
<add name="IdentityEntities" providerName="System.Data.SqlClient" connectionString="Server=YourServer;Database=Identiydatabase;Trusted_Connection=True;MultipleActiveResultSets=true;" />