C# 实体类型ApplicationRole不是当前上下文模型的一部分。这不是重复的
首先,有很多这样的问题,但这不是重复的,因为我的代码与其他代码确实不同 我想使用Asp.net MVC身份实现成员身份。但我不能容忍这个错误。让我们看看代码: IdentityStratup.csC# 实体类型ApplicationRole不是当前上下文模型的一部分。这不是重复的,c#,asp.net-mvc-4,asp.net-identity,C#,Asp.net Mvc 4,Asp.net Identity,首先,有很多这样的问题,但这不是重复的,因为我的代码与其他代码确实不同 我想使用Asp.net MVC身份实现成员身份。但我不能容忍这个错误。让我们看看代码: IdentityStratup.cs using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; using Microsoft.Owin.Security.Cookies; using Microsoft.AspNet.Identity; u
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);
}
}
}
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();
}
}
}
在Identity文件夹->ApplicationRole.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);
}
}
}
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();
}
}
}
在Identity文件夹->ApplicationUser.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);
}
}
}
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();
}
}
}
模型文件夹中的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; }
}
}
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
名称空间登录.Models
{
公共类登录
{
[必需]
[长度(50)]
[RegularExpression(@“^(?=[a-zA-Z])[-\w.]{0,23}([a-zA-Z\d]|)(?您正在使用名为LoginDBContext
的上下文,但是您的身份数据库上下文名为MyDbContext
将应用程序\u Start
的第三行更改为
MyDbContext db = new MyDbContext();
我尝试了你的建议,但没有成功。谢谢@haim770 3你的代码可能不同,但问题与其他问题相同。基本上,此错误表示你的数据库没有存储角色的表,或者迁移不知道此表。创建创建新迁移查看迁移中添加的内容,运行迁移n数据库。你绝对是对的。非常感谢你的帮助。你是对的,但我确实忘了更改那一行。我做了你的建议,但没有。谢谢。请执行以下操作:打开包管理器控制台,运行enable migrations命令,然后再运行add migration-name Initial,然后你应该找到一个名为Configura的新类在名为Migration的文件夹中,该类中有一个名为Seed的方法,请将添加角色的代码移动到此方法中,然后重试谢谢,我现在正在尝试。当我尝试启用迁移时,出现此错误“不支持使用DbModelBuilder或从使用“数据库优先”或“模型优先”创建的DbContext写入EDMX。EDMX只能从不使用现有DbCompiledModel创建的“代码优先”DbContext获取。“是的,亲爱的,您必须先使用代码,asp.net标识基于“代码优先”而不是“数据库优先”。