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():
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With