【发布时间】:2020-05-16 21:51:22
【问题描述】:
因此,我使用 ASP.Identity 和 EntityFramework 使用代码优先方法制作了 .NET Core 应用程序。 我的 AppUser 类:
public class AppUser : IdentityUser<string>
{
public string Name { get; set; }
public string Surname { get; set; }
public DateTime DateJoined { get; set; }
}
然后我有附加 AppUser 的 Notice.cs 类
public class Notice
{
//... some other props
public string Description { get; set; }
public DateTime DatePosted { get; set; }
public AppUser AppUser { get; set; }
}
还有我的 applicationDbContext
public class ApplicationDbContext : IdentityDbContext<AppUser, Role, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Notice>()
.Property(p => p.NoticeId)
.ValueGeneratedOnAdd();
builder.Entity<AppUser>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
builder.Entity<Role>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
}
public DbSet<Notice> Notices { get; set; }
}
在将通知播种到数据库并将用户分配给他们后,我无法访问 AppUser 属性。 我查看了数据库,可以看到 Notices 表有适当的 AppUserId 列和正确的 ID。但是,每次我尝试从数据库上下文访问通知的 AppUser 属性时,它总是为空。我该如何解决?谢谢!
【问题讨论】:
标签: c# sql-server entity-framework .net-core asp.net-identity