我正在使用带有Identity的MVC。我正在使用来自here的自定义标识。它适用于本地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
}
}
答案 0 :(得分:0)
通过服务器名称(或IP)与remplace YourServer
一起尝试:
<add name="IdentityEntities" providerName="System.Data.SqlClient" connectionString="Server=YourServer;Database=Identiydatabase;Trusted_Connection=True;MultipleActiveResultSets=true;" />