Ich habe eine Anwendung in Asp.NET Core 3.1 MVC mit EF Core und Identität.
Ich habe zwei Tabellen Calls
und AspNetUsers
. AspNetUsers
hat viele Calls
und ein Call
hat einen AspNetUsers
.
Die Struktur der Calls
Tabelle ist in Ordnung, denke ich. Aber jetzt muss ich Calls
von AspNetUsers
.
In CallsController
versuche ich: IList<Call> calls = this.User.Calls;
aber kein Erfolg.
Ich habe es versucht:
IList<Call> calls = this._context.Calls.Where(x => x.UserId == this._userManager.GetUserId(this.User)).ToList();
Ich habe Erfolg. Aber ist es richtig?
Also, in der Anwendung habe ich Identitätsklassen und einen ApplicationUser
wie diesen:
public class ApplicationUser : IdentityUser
{
public virtual IList<Call> Calls { get; set; }
}
Und in der Startup
Klasse in der ConfigureServices
Methode:
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Was ist der bessere Weg, um Anrufe von AspNetUsers zu erhalten? Vielen Dank!
Sie können ApplicationUser
wie ApplicationUser
einstellen:
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; }
}
Fügen Sie in ApplicationDbContext Folgendes hinzu:
public virtual DbSet<Call> Calls { get; set; } //add this line
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
Anschließend können Sie den Anruf des aktuellen Benutzers abfragen, indem Sie:
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 sollte sein
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Call> Calls { get; set; }
}
Anrufentität sollte sein
public class Call
{
public int ID { get; set; }
//...
public string ApplicationUserId { get; set; }
[ForeignKey(nameof(ApplicationUserId))]
public virtual ApplicationUser ApplicationUser { get; set; }
}
und natürlich sollten Sie die OnModeCreating-Methode in Ihrem DbContext wie folgt überschreiben
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..
}
Laden Sie abschließend alle Ihre Anrufe in die Calls-Auflistung von ApplicationUser
var user = await _context.ApplicationUsers.FindAsync(_userManager.GetUserId(this.User));
await context.Entry(user)
.Collection(x => x.Calls)
.LoadAsync();
Jetzt haben Sie alle Ihre Anrufe in die Anrufsammlung des aktuellen Benutzers geladen.