Ho un'applicazione in Asp.NET Core 3.1 MVC con EF Core e Identity.
Ho due tabelle Calls
e AspNetUsers
. AspNetUsers
ha molte Calls
e una Call
ha un AspNetUsers
.
La struttura della tabella Calls
va bene, penso. Ma ora ho bisogno di ricevere Calls
da AspNetUsers
.
In CallsController
sto provando: IList<Call> calls = this.User.Calls;
ma nessun successo.
Provai:
IList<Call> calls = this._context.Calls.Where(x => x.UserId == this._userManager.GetUserId(this.User)).ToList();
Ho successo Ma è corretto?
Quindi, nell'applicazione ho classi di identità e un ApplicationUser
come questo:
public class ApplicationUser : IdentityUser
{
public virtual IList<Call> Calls { get; set; }
}
E nella classe Startup
nel metodo ConfigureServices
:
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Quindi, qual è il modo migliore per ricevere chiamate da AspNetUsers? Grazie!
È possibile impostare ApplicationUser
come:
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; }
}
In ApplicationDbContext, aggiungi:
public virtual DbSet<Call> Calls { get; set; } //add this line
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
Quindi è possibile eseguire una query sulla chiamata dell'utente corrente:
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 dovrebbe essere
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
L'entità chiamata dovrebbe essere
public class Call
{
public int ID { get; set; }
//...
public string ApplicationUserId { get; set; }
[ForeignKey(nameof(ApplicationUserId))]
public virtual ApplicationUser ApplicationUser { get; set; }
}
e, ovviamente, dovresti sovrascrivere il metodo OnModeCreating nel tuo DbContext in questo modo
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..
}
e infine, carica tutte le tue chiamate nella raccolta Chiamate di ApplicationUser
var user = await _context.ApplicationUsers.FindAsync(_userManager.GetUserId(this.User));
await context.Entry(user)
.Collection(x => x.Calls)
.LoadAsync();
Ora, tutte le chiamate vengono caricate nella raccolta Chiamate dell'utente corrente.