【问题标题】:Many to many relationship with ASP.NET AspNetUser table gets ignored与 ASP.NET AspNetUser 表的多对多关系被忽略
【发布时间】:2018-02-18 23:43:53
【问题描述】:

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

ASP.NET 身份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;
    }
}

我的Project

[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的东西正在起作用......

有人可以告诉我我在这里缺少什么吗? (我用谷歌搜索了它,我多次删除了整个数据库,我尝试了我找到的所有东西,没有运气......)

【问题讨论】:

    标签: asp.net entity-framework


    【解决方案1】:

    我会将 ApplicationUser 实体与项目中的任何逻辑分开,并创建另一个名为 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
    
    }
    

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

    [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 类中的两个实体添加数据库集。 现在你想得到一个有项目列表的人,反之亦然。为此,我们使用包含:

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

    //您可以使用 applicationUser 实体代替 person,但随着项目的发展会发生一些不好的事情。

    【讨论】:

    • 我不需要额外的桌子,但如果这是唯一的方法......视线......非常感谢!
    • 不用担心,它只是那个应用程序中的用户用户拥有非常敏感的信息,你不想在你的应用程序中跳转,比如密码散列的安全声明。让该类处理应用程序所需的安全性和人员处理逻辑。欢迎您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多