身份用户 FK
Identity User FK
我在 Asp.NET Core 3.1 MVC 中有一个应用程序,带有 EF Core 和 Identity。
我有两个 table Calls
和 AspNetUsers
。 AspNetUsers
有很多 Calls
一个 Call
有一个 AspNetUsers
.
Calls
table 结构我觉得还可以。但是现在我需要从 AspNetUsers
.
得到 Calls
在 CallsController
我正在尝试:IList<Call> calls = this.User.Calls;
但没有成功。
我试过:
IList<Call> calls = this._context.Calls.Where(x => x.UserId == this._userManager.GetUserId(this.User)).ToList();
我成功了。但它是正确的吗?
所以,在应用程序中我有身份 classes 和一个 ApplicationUser
像这样:
public class ApplicationUser : IdentityUser
{
public virtual IList<Call> Calls { get; set; }
}
并且在Startup
class中在ConfigureServices
方法中:
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
那么,从 AspNetUsers 获得呼叫的更好方法是什么? 谢谢!
您可以设置 ApplicationUser
如:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
Call.cs:
public class Call
{
public int ID { get; set; }
public string name { get; set; }
// other properties
public string UserID { get; set; }
[ForeignKey("UserID")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
在 ApplicationDbContext 中,添加:
public virtual DbSet<Call> Calls { get; set; } //add this line
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
然后可以通过:
查询当前用户的来电
if (User.Identity.IsAuthenticated)
{
var userID = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
var calls = _applicationDbContext.Users.Include(u => u.Calls).First(u => u.Id == userID).Calls.ToList();
//or
var callsa = _applicationDbContext.Calls.Where(p => p.UserID == userID).ToList();
}
ApplicationUser 应该是
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
调用实体应该是
public class Call
{
public int ID { get; set; }
//...
public string ApplicationUserId { get; set; }
[ForeignKey(nameof(ApplicationUserId))]
public virtual ApplicationUser ApplicationUser { get; set; }
}
当然,您应该像这样重写 DbContext 中的 OnModeCreating 方法
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options){}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Call>()
.HasOne(x => x.ApplicationUser)
.WithMany(x => x.Calls);
}
//...DbSets..
}
最后,将所有调用加载到 ApplicationUser
的 Calls 集合中
var user = await _context.ApplicationUsers.FindAsync(_userManager.GetUserId(this.User));
await context.Entry(user)
.Collection(x => x.Calls)
.LoadAsync();
现在,您已将所有通话加载到当前用户的通话集合中。
我在 Asp.NET Core 3.1 MVC 中有一个应用程序,带有 EF Core 和 Identity。
我有两个 table Calls
和 AspNetUsers
。 AspNetUsers
有很多 Calls
一个 Call
有一个 AspNetUsers
.
Calls
table 结构我觉得还可以。但是现在我需要从 AspNetUsers
.
Calls
在 CallsController
我正在尝试:IList<Call> calls = this.User.Calls;
但没有成功。
我试过:
IList<Call> calls = this._context.Calls.Where(x => x.UserId == this._userManager.GetUserId(this.User)).ToList();
我成功了。但它是正确的吗?
所以,在应用程序中我有身份 classes 和一个 ApplicationUser
像这样:
public class ApplicationUser : IdentityUser
{
public virtual IList<Call> Calls { get; set; }
}
并且在Startup
class中在ConfigureServices
方法中:
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
那么,从 AspNetUsers 获得呼叫的更好方法是什么? 谢谢!
您可以设置 ApplicationUser
如:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
Call.cs:
public class Call
{
public int ID { get; set; }
public string name { get; set; }
// other properties
public string UserID { get; set; }
[ForeignKey("UserID")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
在 ApplicationDbContext 中,添加:
public virtual DbSet<Call> Calls { get; set; } //add this line
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
然后可以通过:
查询当前用户的来电if (User.Identity.IsAuthenticated)
{
var userID = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
var calls = _applicationDbContext.Users.Include(u => u.Calls).First(u => u.Id == userID).Calls.ToList();
//or
var callsa = _applicationDbContext.Calls.Where(p => p.UserID == userID).ToList();
}
ApplicationUser 应该是
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
调用实体应该是
public class Call
{
public int ID { get; set; }
//...
public string ApplicationUserId { get; set; }
[ForeignKey(nameof(ApplicationUserId))]
public virtual ApplicationUser ApplicationUser { get; set; }
}
当然,您应该像这样重写 DbContext 中的 OnModeCreating 方法
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options){}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Call>()
.HasOne(x => x.ApplicationUser)
.WithMany(x => x.Calls);
}
//...DbSets..
}
最后,将所有调用加载到 ApplicationUser
的 Calls 集合中 var user = await _context.ApplicationUsers.FindAsync(_userManager.GetUserId(this.User));
await context.Entry(user)
.Collection(x => x.Calls)
.LoadAsync();
现在,您已将所有通话加载到当前用户的通话集合中。