简体   繁体   English

ASP.NET MVC如何使用ApplicationUser和其他域类之间的多对多关系

[英]ASP.NET MVC How to Use Many to Many Relationship Between ApplicationUser and Other Domain Class

I am currently working on a project using ASP.NET MVC, using Entity Framework and the code-first approach.我目前正在使用 ASP.NET MVC,使用实体框架和代码优先方法开发一个项目。 Precursor, I am pretty new to all of these so some things still don't make a whole lot of sense to me.先行者,我对所有这些都很陌生,所以有些事情对我来说仍然没有多大意义。

I have made a many-to-many relationship between the ApplicationUser class and the Project class as such:我在 ApplicationUser 类和 Project 类之间建立了多对多关系,如下所示:

IdentityModels.cs: IdentityModels.cs:

public class ApplicationUser : IdentityUser
{
    public ICollection<UserProject> UserProjects { get; set; }
}

Project.cs:项目.cs:

public class Project
{
    [...]
    public ICollection<UserProject> UserProjects { get; set; }
}

UserProject.cs:用户项目.cs:

public class UserProject
{
    [Key, Column(Order = 1)]
    public string UserId { get; set; }
    [Key, Column(Order = 2)]
    public int ProjectId { get; set; }
    public ApplicationUser User { get; set; }
    public Project Project { get; set; }
}

I created a DbSet of UserProject to my ApplicationDbContext and created a migration for this.我创建了一个 UserProject 的 DbSet 到我的 ApplicationDbContext 并为此创建了一个迁移。

With this being said, my question is how would I go about using this relationship?话虽如此,我的问题是我将如何使用这种关系? More specifically, if I wanted to display only the projects that the signed in user is assigned to, how could I go about doing that?更具体地说,如果我只想显示已登录用户分配到的项目,我该怎么做?

Also, my current assumption is that if I wanted to assign projects to users, I would ultimately just need to make a new UserProject object with the given User ID and the Project IDs I want to assign them with.此外,我目前的假设是,如果我想将项目分配给用户,我最终只需要使用给定的用户 ID 和我想要分配给它们的项目 ID 创建一个新的 UserProject 对象。 Would that be how it would work?这会是它的工作方式吗?

Is my configuration of this relationship set up in a good way or should I consider changing it to get the above functionality?我对这种关系的配置是否设置得很好,还是应该考虑更改它以获得上述功能?

Any and all help is appreciated.任何和所有的帮助表示赞赏。 Thanks!谢谢!

Starting from the last question, about the configuration从上一个问题开始,关于配置

EF6 supports many-to-many relations so there's no need to manually create the joining table UserProject You can simply do this EF6 支持多对多关系,因此无需手动创建连接表UserProject您可以简单地执行此操作

IdentityModels.cs: IdentityModels.cs:

public class ApplicationUser : IdentityUser
{
    public ICollection<Project> Projects { get; set; }
}

Project.cs:项目.cs:

public class Project
{
    [...]
    public ICollection<ApplicationUser> Users { get; set; }
}

And that's it.就是这样。

Manually adding the joining table can still work, but you will then have to manage the CRUD operations by yourself.手动添加连接表仍然可以工作,但您必须自己管理 CRUD 操作。 If you still want to add it yourself, leave the 2 models as they are and reorder the properties in the joining table to honor the convention or the DB might get messy, in that case edit it to如果您仍然想自己添加它,请保留 2 个模型,并重新排列连接表中的属性以遵守约定,否则数据库可能会变得混乱,在这种情况下将其编辑为

{
    [Key, Column(Order = 1)]
    public string UserId { get; set; }
    public ApplicationUser User { get; set; }

    [Key, Column(Order = 2)]
    public int ProjectId { get; set; }
    public Project Project { get; set; }
}

To get a user's list of projects you can simply access the list in the model (make sure it's included when querying the user from the DB)要获取用户的项目列表,您可以简单地访问模型中的列表(确保在从数据库查询用户时包含它)


Your assumption is correct, depending on which approach you will take, either create a new UserProject containing the Ids, or add a Project to User.Projects list.您的假设是正确的,这取决于您将采用哪种方法,或者创建一个包含 Id 的新UserProject ,或者将一个Project添加到User.Projects列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net MVC 如何在ApplicationUser和我自己的类之间建立一对多的关系? - ASP.Net MVC How to create a one-to-many relationship between the ApplicationUser and my own class? 如何与自定义 ApplicationUser class MVC 创建多对多关系? - How to create many-to-many relationship with custom ApplicationUser class MVC? 如何使用ASP.NET MVC 5在EF中实现多对多关系 - How to implement many to many relationship in EF with ASP.NET MVC 5 与ApplicationUser具有多对多关系的.Net MVC 5查询模型 - .Net MVC 5 querying models that have a many-to-many-relationship with ApplicationUser 如何在ASP.NET MVC中设置一对多关系? - How to setup one to many relationship in asp.net mvc? 如何在ASP.NET MVC 5中创建HtmlDropdownListfor而不使用一对多关系中的模型视图 - How to create HtmlDropdownListfor in asp.net mvc 5 without use of modelviews in one to many relationship ASP.net MVC实体框架更新多对多关系 - ASP.net MVC Entity framework update Many to Many relationship 使用EF 5和ASP.NET MVC 4为多对多关系添加值 - Add value to Many to Many relationship with EF 5 & ASP.NET MVC 4 CheckBoxList用于ASP.NET MVC中的多对多关系 - CheckBoxList for many-to-many relationship in ASP.NET MVC ASP.NET MVC 2 LINQ to SQL多对多关系 - ASP.NET MVC 2 linq to sql many to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM