【问题标题】:WithRequired() No Extension Method Defined [duplicate]WithRequired() 未定义扩展方法 [重复]
【发布时间】:2016-11-17 19:42:16
【问题描述】:

过去三个小时我一直在寻找,试图弄清楚发生了什么,最后不得不分解并发布一个问题。

我正在尝试将 WithRequired()/HasRequired() 添加到我的 ApplicationUser 模型构建器中,但它告诉我它没有定义。

我只是在这里完全无知,还是随着 .Net Core/EF Core 框架而改变?

应用用户:

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    public ICollection<Following> Followers { get; set; }

    public ICollection<Following> Followees { get; set; }

    public ApplicationUser()
    {
        Followers = new Collection<Following>();
        Followees = new Collection<Following>();
    }
}

Following.cs:

public class Following
{
    [Key]
    [Column(Order = 1)]
    public string FollowerId { get; set; }

    [Key]
    [Column(Order = 2)]
    public string FolloweeId { get; set; }

    public ApplicationUser Follower { get; set; }
    public ApplicationUser Followee { get; set; }
}

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Gig> Gigs { get; set; }
    public DbSet<Genre> Genres { get; set; }

    public DbSet<Attendance> Attendances { get; set; }

    public DbSet<Following> Followings { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<Attendance>()
            .HasKey(c => new { c.GigId, c.AttendeeId });

        builder.Entity<ApplicationUser>()
            .HasMany(u => u.Followers);

        builder.Entity<ApplicationUser>()
            .HasMany(u => u.Followees);
    }
}

当我尝试添加 WithRequired()/HasRequired() 时出错:

【问题讨论】:

  • 看起来他们可能改变了你的 API:stackoverflow.com/questions/31943779/…
  • @dshapiro 实际上我只是自己偶然发现的。谢谢。
  • 为了帮助其他人,请随意写下您自己的问题的答案,以及您为使其正常工作所做的工作。

标签: c# asp.net-mvc asp.net-identity entity-framework-core


【解决方案1】:

你可以使用

builder.Entity<ApplicationUser>()
        .HasMany(u => u.Followers).WithOne(tr => tr.Follower).IsRequired()

插入

builder.Entity<ApplicationUser>()
        .HasMany(u => u.Followers).WithRequired(tr => tr.Follower)

【讨论】:

    【解决方案2】:

    你需要这样写才能得到WithRequired()(根据你的Following类)

    builder.Entity<Following>().HasRequired(u=>u.Follower).WithMany(u=>u.Followers);
    

    请重写实体以获得正确的输出。

    public class ApplicationUser : IdentityUser{
      [Required]
      [StringLength(100)]
      public string Name { get; set; }
    
      public Following Follower { get; set; }
    }
    
    public class Following{
        [Key]
        [Column(Order = 1)]
        public string FollowerId { get; set; }
    
        [Key]
        [Column(Order = 2)]
        public string FolloweeId { get; set; }
    
        public ICollection<ApplicationUser> Followers { get; set; }
    }
    

    那么OnModelCreating就是

    protected override void OnModelCreating(ModelBuilder builder)
    {
            builder.Entity<ApplicationUser>()
        .HasRequired(u=>u.Follower)
            .HasMany(u => u.Followers);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2021-05-21
      • 2018-09-02
      相关资源
      最近更新 更多