2016-07-14 107 views
1

我一直在寻找过去三个小时试图弄清楚发生了什么,最后不得不分解并发布一个问题。WithRequired()没有扩展方法定义

我想向ApplicationUser的模型构建器添加WithRequired()/ HasRequired(),但它告诉我它没有被定义。

我在这里完全无知,还是因.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

@ dshapiro我实际上只是偶然发现了这一点。谢谢。 – RugerSR9

+0

为了帮助他人,请随时为自己的问题写一个答案,并用你所做的工作来解决问题。 – dshapiro

回答

0

你需要写这样得到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); 
} 
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) 
相关问题