如何通过ef-code-first在AspNetUsers表上创建与其它实体多对多关联关系?

2026-03-30 11:331阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计405个文字,预计阅读时间需要2分钟。

如何通过ef-code-first在AspNetUsers表上创建与其它实体多对多关联关系?

我是Identity framework和Code First的新手。现在我在VS 2015上创建一个MVC5项目时,使用默认的项目设置。运行项目时生成的默认表结构很好,但我希望它能与其他项目一起生成一个名为tbSkills(Id, Sk)的表。

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

如何通过ef-code-first在AspNetUsers表上创建与其它实体多对多关联关系?

我运行项目时生成的默认表格很好,但我希望它与其他项目一起生成一个名为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; } }

它以此错误结束:

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

尝试添加技能ID

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 int SkillId {get;set;} public virtual ICollection<tbSkill> Skills { get; set; } }

本文共计405个文字,预计阅读时间需要2分钟。

如何通过ef-code-first在AspNetUsers表上创建与其它实体多对多关联关系?

我是Identity framework和Code First的新手。现在我在VS 2015上创建一个MVC5项目时,使用默认的项目设置。运行项目时生成的默认表结构很好,但我希望它能与其他项目一起生成一个名为tbSkills(Id, Sk)的表。

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

如何通过ef-code-first在AspNetUsers表上创建与其它实体多对多关联关系?

我运行项目时生成的默认表格很好,但我希望它与其他项目一起生成一个名为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; } }

它以此错误结束:

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

尝试添加技能ID

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 int SkillId {get;set;} public virtual ICollection<tbSkill> Skills { get; set; } }