gpt4 book ai didi

ef-code-first - EntityType 'IdentityUserLogin' 没有定义键。定义此 EntityType 的键

转载 作者:行者123 更新时间:2023-12-03 04:50:37 25 4
gpt4 key购买 nike

我正在使用 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/

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