2016-07-14 4 views
1

Я искал последние три часа, пытаясь выяснить, что происходит, и, наконец, пришлось сломать и отправить вопрос.WithRequired() Нет метода расширения

Я пытаюсь добавить WithRequired()/HasRequired() в мой конструктор моделей для ApplicationUser, но он говорит мне, что он не определен.

Я просто совершенно незнакомец здесь, или это изменилось с каркасами .Net Core/EF Core?

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

Ошибка при попытке добавить WithRequired()/HasRequired(): none

+1

Похоже, что они изменили API на вас: http://stackoverflow.com/questions/31943779/ef-7-beta-6-entities-with-one-to-many-relationships-in-ef-7 – dshapiro

+0

@ Дшапиро. Я просто наткнулся на это сам. Благодарю. – RugerSR9

+0

Чтобы помочь другим, не стесняйтесь писать ответ на свой вопрос с тем, что вы сделали, чтобы получить эту работу. – dshapiro

ответ

0

Вам нужно написать так, чтобы получить WithRequired() (согласно вашему Following класса)

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

Перепишите Entity, чтобы получить надлежащую производительность.

т.е.

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

вы можете использовать

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

InstEd из

builder.Entity<ApplicationUser>() 
     .HasMany(u => u.Followers).WithRequired(tr => tr.Follower) 
Смежные вопросы