10

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

RugerSR9
  • 448
  • 1
  • 4
  • 17
  • 1
    Looks like maybe they changed the API on you: http://stackoverflow.com/questions/31943779/ef-7-beta-6-entities-with-one-to-many-relationships-in-ef-7 – dshapiro Jul 14 '16 at 19:27
  • @dshapiro I actually just stumbled across that myself. Thanks. – RugerSR9 Jul 14 '16 at 19:37
  • In order to assist others, feel free to write an answer to your own question with what you did to get this working. – dshapiro Jul 15 '16 at 18:04

2 Answers2

18

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)
Hassan Golab
  • 189
  • 1
  • 3
0

You need to write like this to get WithRequired()(As per your Following class)

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

Please rewrite the Entity to get proper output.

i.e

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

Then OnModelCreating would be

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