- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Entity Framework Code First 和 MVC 5。当我使用个人用户帐户身份验证创建应用程序时,我获得了一个帐户 Controller 以及所有必需的类和代码,需要使个人用户帐户身份验证发挥作用。
已经到位的代码如下:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DXContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
但后来我继续使用代码创建了自己的上下文,所以我现在也有以下内容:
public class DXContext : DbContext
{
public DXContext() : base("DXContext")
{
}
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<IdentityRole> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Paintings> Paintings { get; set; }
}
最后,我有以下种子方法来添加一些数据供我在开发时使用:
protected override void Seed(DXContext context)
{
try
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
context.SaveChanges();
if (!context.Users.Any(u => u.UserName == "James"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "James" };
manager.Create(user, "ChangeAsap1@");
manager.AddToRole(user.Id, "Admin");
}
context.SaveChanges();
string userId = "";
userId = context.Users.FirstOrDefault().Id;
var artists = new List<Artist>
{
new Artist { FName = "Salvador", LName = "Dali", ImgURL = "http://i62.tinypic.com/ss8txxn.jpg", UrlFriendly = "salvador-dali", Verified = true, ApplicationUserId = userId },
};
artists.ForEach(a => context.Artists.Add(a));
context.SaveChanges();
var paintings = new List<Painting>
{
new Painting { Title = "The Persistence of Memory", ImgUrl = "http://i62.tinypic.com/xx8tssn.jpg", ArtistId = 1, Verified = true, ApplicationUserId = userId }
};
paintings.ForEach(p => context.Paintings.Add(p));
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
我的解决方案构建得很好,但是当我尝试访问需要访问数据库的 Controller 时,出现以下错误:
DX.DOMAIN.Context.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
DX.DOMAIN.Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
我做错了什么?是因为我有两个上下文吗?
更新
阅读 Augusto 的回复后,我选择了选项 3。这是我的 DXContext 类现在的样子:
public class DXContext : DbContext
{
public DXContext() : base("DXContext")
{
// remove default initializer
Database.SetInitializer<DXContext>(null);
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Painting> Paintings { get; set; }
public static DXContext Create()
{
return new DXContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
}
public DbQuery<T> Query<T>() where T : class
{
return Set<T>().AsNoTracking();
}
}
我还添加了一个User.cs
和一个Role.cs
类,它们看起来像这样:
public class User
{
public int Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
}
public class Role
{
public int Id { set; get; }
public string Name { set; get; }
}
我不确定是否需要用户的密码属性,因为默认的 ApplicationUser 具有该属性和一堆其他字段!
无论如何,上述更改构建得很好,但在运行应用程序时我再次收到此错误:
Invalid Column name UserId
UserId
是我的 Artist.cs
最佳答案
在我的例子中,我正确地从 IdentityDbContext 继承(使用我自己的自定义类型和定义的键),但无意中删除了对基类的 OnModelCreating 的调用:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // I had removed this
/// Rest of on model creating here.
}
然后修复了身份类中丢失的索引,然后我可以生成迁移并适本地启用迁移。
关于ef-code-first - EntityType 'IdentityUserLogin' 没有定义键。定义此 EntityType 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28531201/
这个问题已经有答案了: The entity type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin' requires a
这个问题在这里已经有了答案: The entity type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin' requir
我正在尝试构建一个 asp.net MVC-5 项目 当我突然出现这个错误时 Error 1 'Microsoft.AspNet.Identity.EntityFramework.IdentityUs
所以我有两个上下文,我正在使用 mycontext,但即使 mycontext 继承自 IdentityDbcontext,我也会不断收到错误消息。我尝试了在这里找到的各种修复程序,但都没有用 我在启
我正在使用 Entity Framework Code First 和 MVC 5。当我使用个人用户帐户身份验证创建应用程序时,我获得了一个帐户 Controller 以及所有必需的类和代码,需要使个
我有一个使用 EF7 的 ASP.NET5 MVC 应用程序。到目前为止一切正常,我能够在数据库中添加迁移和持久数据。现在,在将身份添加到我的数据层项目后,我在尝试添加新迁移时遇到此错误: The e
我们的应用程序不需要身份使用的过多“登录”和“声明”功能。如果没有在数据库中创建这些表 simlpy 就好了,但我不想重新实现所有身份类... 我猜是这样的 public ApplicationDbC
我是一名优秀的程序员,十分优秀!