【发布时间】:2016-11-17 19:42:16
【问题描述】:
过去三个小时我一直在寻找,试图弄清楚发生了什么,最后不得不分解并发布一个问题。
我正在尝试将 WithRequired()/HasRequired() 添加到我的 ApplicationUser 模型构建器中,但它告诉我它没有定义。
我只是在这里完全无知,还是随着 .Net Core/EF Core 框架而改变?
应用用户:
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);
}
}
【问题讨论】:
-
看起来他们可能改变了你的 API:stackoverflow.com/questions/31943779/…
-
@dshapiro 实际上我只是自己偶然发现的。谢谢。
-
为了帮助其他人,请随意写下您自己的问题的答案,以及您为使其正常工作所做的工作。
标签: c# asp.net-mvc asp.net-identity entity-framework-core