Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WithRequired() No Extension Method Defined [duplicate]

I have been searching for the past three hours trying to figure out what is going on and finally had to break down and post a question.

I am trying to add WithRequired()/HasRequired() to my model builder for ApplicationUser, but it tells me that it is not defined.

Am I just being completely ignorant here, or has this changed with the .Net Core/EF Core frameworks?

ApplicationUser:

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);
    }
}

Error when I try to add WithRequired()/HasRequired(): none

like image 546
RugerSR9 Avatar asked Jul 14 '16 18:07

RugerSR9


1 Answers

you can use

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

insted of

builder.Entity<ApplicationUser>()
        .HasMany(u => u.Followers).WithRequired(tr => tr.Follower)
like image 87
Hassan Golab Avatar answered Nov 04 '22 21:11

Hassan Golab