Место общения программистов C#
Anonymous
Получить данные из базы данных SQL с помощью DbSet
Сообщение
Anonymous » 30 сен 2024, 11:51
У меня есть следующий DataBaseContext:
Код: Выделить всё
modelBuilder.Entity()
.Property(a => a.Name)
.IsRequired();
modelBuilder.Entity()
.Property(a => a.Type)
.IsRequired();
modelBuilder.Entity()
.Property(a => a.Title)
.IsRequired();
modelBuilder.Entity().HasOne(x => x.Artist);
modelBuilder.Entity().HasOne(x => x.AlbumType);
modelBuilder.Entity().HasMany(x => x.Songs);
modelBuilder.Entity()
.Property(a => a.Title)
.IsRequired();
modelBuilder.Entity().HasOne(x => x.Artist);
В моих услугах:
Код: Выделить всё
var album = await unitOfWork.AlbumRepository.GetByIDAsync(id);
try
{
_context.Entry(album).Reference(a => a.Artist).Load();
_context.Entry(album).Reference(a => a.AlbumType).Load();
_context.Entry(album).Collection(a => a.Songs).Load();
}
catch (Exception ex)
{
throw ex;
}
return album;
И в моем UnitOfWork:
Код: Выделить всё
public virtual async Task GetByIDAsync(object id)
{
return await dbSet.FindAsync(id);
}
Теперь, когда я хочу отобразить сведения об альбоме, я хочу отобразить песни. Все работает нормально.
Но я не могу получить информацию об исполнителе этой песни. Кто-нибудь может мне помочь?
Подробнее здесь:
https://stackoverflow.com/questions/790 ... sing-dbset
1727686288
Anonymous
У меня есть следующий DataBaseContext: [code] modelBuilder.Entity() .Property(a => a.Name) .IsRequired(); modelBuilder.Entity() .Property(a => a.Type) .IsRequired(); modelBuilder.Entity() .Property(a => a.Title) .IsRequired(); modelBuilder.Entity().HasOne(x => x.Artist); modelBuilder.Entity().HasOne(x => x.AlbumType); modelBuilder.Entity().HasMany(x => x.Songs); modelBuilder.Entity() .Property(a => a.Title) .IsRequired(); modelBuilder.Entity().HasOne(x => x.Artist); [/code] В моих услугах: [code]var album = await unitOfWork.AlbumRepository.GetByIDAsync(id); try { _context.Entry(album).Reference(a => a.Artist).Load(); _context.Entry(album).Reference(a => a.AlbumType).Load(); _context.Entry(album).Collection(a => a.Songs).Load(); } catch (Exception ex) { throw ex; } return album; [/code] И в моем UnitOfWork: [code]public virtual async Task GetByIDAsync(object id) { return await dbSet.FindAsync(id); } [/code] Теперь, когда я хочу отобразить сведения об альбоме, я хочу отобразить песни. Все работает нормально. Но я не могу получить информацию об исполнителе этой песни. Кто-нибудь может мне помочь? Подробнее здесь: [url]https://stackoverflow.com/questions/79038548/get-data-from-sql-database-using-dbset[/url]