本文介绍了与ASP.NET AspNetUser表的多对多关系被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用户可以是多个项目的成员,而一个项目可以有多个成员.

One user can be a member of many projects while a project can have multiple members.

ASP.NET身份 ApplicationUser

ASP.NET Identity ApplicationUser

public class ApplicationUser : IdentityUser
{
    [Display(Name = "Projekt")]
    public virtual ICollection<Project> Projects { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

我的项目:

[Table("Projects")]
public class Project : IValidatableObject
{
    [Key]
    public int ID { get; set; }
    [Required, StringLength(128), Display(Name = "Projektname"), Index(IsUnique = true)]
    public string Name { get; set; }

    // working one-to-many relation
    [Display(Name = "Projektleiter")]
    public string LeaderID { get; set; }
    [ForeignKey("LeaderID"), Display(Name = "Projektleiter")]
    public virtual ApplicationUser ApplicationUser { get; set; }

    // many-to-many relation gets ignored
    [Display(Name = "Mitarbeiter")]
    public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        return new List<ValidationResult>();
    }
}

未创建多对多表.整个关系将被忽略.但是LeaderId事情正在起作用...

No Many-to-Many table is created. The whole relation gets ignored. But the LeaderID thing is working ...

有人可以告诉我我在这里想念的东西吗?(我用它搜索了地狱,我多次删除了整个数据库,我尝试了所有发现的事情,没有运气...)

Can somebody tell me what I am missing here? (I googled the hell out of it, I deleted the whole database multiple times, I tried everything I found, no luck ...)

推荐答案

我将把ApplicationUser实体与项目中使用的任何逻辑分开,并创建另一个名为Person的实体.

I would keep the ApplicationUser entity separated from whatever logic you have with the project and create another entity called Person.

public class Person
{
//Constuctor : always intantiate lists in ctor
public Person()
{
       Projects = new List<Project>();
}

 public int Id { get; set; }
 public string IdentityId { get; set; } //gets guid from AppUser table
 public ApplicationUser Identity { get; set; }  // navigation property
 public List<Project> Projects { get; set; }
 //public int ProjectId {get; set;}//-----optional

}

所以在项目中知道我们做同样的事情:

so in projects know we do the same thing:

[Table("Projects")]
public class Project : IValidatableObject
{
        //Constuctor : always intantiate lists in ctor
    public Project()
    {
           Persons = new List<Person>();
    }
    [Key]
    public int ID { get; set; }
    [Required, StringLength(128), Display(Name = "Projektname"), Index(IsUnique = true)]
    public string Name { get; set; }

    public string PersonId { get; set; }  //nav property
    public List<Persons> Persons { get; set; }
    public Person Person { get; set; } //context will be aware that this is fk

   [Display(Name = "Projektleiter")]
   public string LeaderID { get; set; }




    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        return new List<ValidationResult>();
    }
}

为dbcontext类中的两个实体添加数据库集.现在,您想得到一个带有项目列表的人,反之亦然.为此,我们使用include:

add db sets for both entities in the dbcontext class.Now you want to get a person with a list of projects and vice versa. For that we use the include:

var x = myContext.Persons.Include(x => x.Projects);
var d = x.ToList();

///您可以使用applicationUser实体代替person,但是随着项目的发展,坏事会发生.

//you can use the applicationUser entity instead of person but bad things happen as the project grows.

这篇关于与ASP.NET AspNetUser表的多对多关系被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:55
查看更多