IdentityUserLogin< string>'添加迁移时需要主键被定义为错误 [英] IdentityUserLogin<string>' requires a primary key to be defined error while adding migration

查看:43
本文介绍了IdentityUserLogin< string>'添加迁移时需要主键被定义为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2个不同的Dbcontext。我想使用2个不同的数据库用户和mycontext。这样做时出现错误。实体类型 Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin需要定义一个主键。我认为IdentityUser有问题,请建议我在哪里可以更改代码,以便添加迁移。

I am using 2 different Dbcontexts. i want to use 2 different databases users and mycontext. While doing that i am getting a error The entity type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin' requires a primary key to be defined. I think there is something wrong with IdentityUser please suggest me where can i change my code so that i can add migration.

我的Dbcontext类:

My Dbcontext class:

 class MyContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<PostTag>()
            .HasKey(t => new { t.PostId, t.TagId });

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Post)
            .WithMany(p => p.PostTags)
            .HasForeignKey(pt => pt.PostId);

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Tag)
            .WithMany(t => t.PostTags)
            .HasForeignKey(pt => pt.TagId);
    }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public AppUser User {get; set;}
    public string Content { get; set; }

    public List<PostTag> PostTags { get; set; }
}

public class Tag
{
    public string TagId { get; set; }

    public List<PostTag> PostTags { get; set; }
}

public class PostTag
{
    public int PostId { get; set; }
    public Post Post { get; set; }

    public string TagId { get; set; }
    public Tag Tag { get; set; }
}

和AppUser类:

public class AppUser : IdentityUser
{
  //some other propeties
}

当我尝试添加迁移时,发生以下错误。

when I try to Add migration the following error occurs.

The entity type 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin<string>' requires a primary key to be defined.

请提供解决方案以解决该问题。

give me solution to resolve the issue..

推荐答案

要简化链接,请尝试以下操作:

To reduce the link to a nutshell, try this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    ...

有关更多信息,请参见上述链接。

See Above link for more.

这篇关于IdentityUserLogin&lt; string&gt;'添加迁移时需要主键被定义为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆