gpt4 book ai didi

c# - 实体类型 ApplicationRole 不是当前上下文模型的一部分。这不是重复的

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:17 25 4
gpt4 key购买 nike

首先,有很多这样的问题,但这不是重复的,因为我的代码与其他代码确实不同。

我想使用 Asp.net MVC Identity 实现成员资格。但是我无法摆脱这个错误。让我们看一下代码:

IdentityStratup.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);

}
}
}

在身份文件夹下 -> ApplicationRole.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;
}
}
}

在身份文件夹下 -> ApplicationUser.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; }
}
}

模型文件夹中的Login.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-Z\d]|(?<![-.])_)$")]
public string UserName { get; set; }

[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
}
}

模型文件夹中的Register.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-Z\d]|(?<![-.])_)$")]
public string Username { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }

[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{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();

关于c# - 实体类型 ApplicationRole 不是当前上下文模型的一部分。这不是重复的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com