实体类型应用程序角色不是当前上下文的模型的一部分.这不会重复



首先,有很多这样的问题,但这没有重复,因为我的代码比其他代码非常不同。

我想使用MVC 标识实现成员资格 Asp.net。但是我不能骑这个错误。让我们看一下代码:

身份分层.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.AspNet.Identity;
using Login.Models;
[assembly: OwinStartup(typeof(Login.App_Start.Startup))]
namespace Login.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
app.CreatePerOwinContext(MyDbContext.Create);
}
}
}

在"标识文件夹"-"应用程序角色">下.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Login.Identity
{
public class ApplicationRole : IdentityRole
{
public string Description { get; set; }
public ApplicationRole ()
{
}
public ApplicationRole (string roleName, string description) : base(roleName)
{
this.Description = description;
}
}
}

在"标识文件夹"-">应用程序用户"下.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
namespace Login.Identity
{
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public string Surname { get; set; }
}
}

登录.cs在"模型"文件夹中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Login.Models
{
public class Login
{
[Required]
[StringLength(50)]
[RegularExpression(@"^(?=[a-zA-Z])[-w.]{0,23}([a-zA-Zd]|(?<![-.])_)$")]
public string UserName { get; set; }
[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{8,}$")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
}
}

注册.cs在"模型"文件夹中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Login.Models
{
public class Register
{
[Key]
[Required]
public int ID { get; set; }
[Required]
[StringLength(50)]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$")]
public string Name { get; set; }
[Required]
[StringLength(50)]
[RegularExpression(@"^(([A-za-z]+[s]{1}[A-za-z]+)|([A-Za-z]+))$")]
public string Surname { get; set; }
[Required]
[StringLength(50)]
[RegularExpression(@"^(?=[a-zA-Z])[-w.]{0,23}([a-zA-Zd]|(?<![-.])_)$")]
public string Username { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{8,}$")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{8,}$")]
[Compare("Password")]
public string PasswordConfirmation { get; set; }
}
}

Global.asasx

using Login.Identity;
using Login.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Login
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
MyDbContext db = new MyDbContext();
RoleStore<ApplicationRole> roleStore = new RoleStore<ApplicationRole>(db);
RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(roleStore);

if (!roleManager.RoleExists("Admin"))
{
ApplicationRole adminRole = new ApplicationRole("Admin", "System Admnistrator");
roleManager.Create(adminRole);
}
if (!roleManager.RoleExists("User"))
{
ApplicationRole userRole = new ApplicationRole("User", "System Contraint User");
roleManager.Create(userRole);
}

}
}
}

MyDbContext.cs

using Login.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Login.Models
{
public class MyDbContext : IdentityDbContext<ApplicationUser>
{
public MyDbContext() : base("name=LoginDBContext")
{
}
public DbSet<Categories> Categories { get; set; }
public static MyDbContext Create()
{
return new MyDbContext();
}
}
}

我给了你完整的申请。我无法解决这个问题标题中提到的这个问题。当我运行应用程序时,在该代码行的global.asasx文件中抛出该可执行文件if (!roleManager.RoleExists("Admin")).你怎么看这个问题?

您使用的是名为LoginDBContext的上下文,但是您的身份数据库上下文名为MyDbContext

Application_Start的第三行更改为

MyDbContext db = new MyDbContext();

相关内容

最新更新