ef-code-first – 如何首先使用代码向Identity默认表AspNetUsers添加多对多关系?

我是Identity framework和Code First方法的新手.现在我在VS 2015上创建一个新的MVC5项目时使用认项目.

我运行项目时生成认表格很好,但我希望它与其他项目一起生成一个名为tbSkills(Id,SkillName)的新表格,它必须与AspNetUsers有多对多的关系.

那么我要写的地方和内容要做到这一点?

ps:我在遵循教程后也将Id类型从字符串更改为int,它运行正常.

这是我在IdentityModels.cs文件中的类ApplicationUser中最后尝试过的:

public class ApplicationUser : IdentityUser<int,MyUserLogin,MyUserRole,MyUserClaim>,IUser<int>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,int> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationoptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    // // This following is my added code 
    public ApplicationUser()
    {
        Skills = new HashSet<tbSkill>();
    }

    public virtual ICollection<tbSkill> Skills { get; set; }
}

public class tbSkill
{
    public tbSkill()
    {
        Users = new HashSet<ApplicationUser>();
    }

    public int Id;
    public string SkillName;

    public virtual ICollection<ApplicationUser> Users { get; set; }
}

它以此错误结束:

enter image description here

我已经研究过很多文章,但没有成功.

解决方法

尝试添加技能ID

public class ApplicationUser : IdentityUser<int,DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    // // This following is my added code 
    public ApplicationUser()
    {
        Skills = new HashSet<tbSkill>();
    }
    public int SkillId {get;set;}
    public virtual ICollection<tbSkill> Skills { get; set; }
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....