[英]Many-to-Many Entity Framework Core 6 (junction table not generated)
我剛剛開始使用Entity Framework Core 6 。 我正在使用具有多對多關系的示例數據庫。
我在 SQL 服務器上創建了我的數據庫。 我創建了三個表:Service、Document、ServiceDocs(用作連接表)。
然后我做了:
scaffolf-dbcontext
除了聯結表 ServiceDocs 之外,這兩個類都已生成。 我的問題是:如何在沒有聯結表的類的情況下向聯結表添加元素並從中獲取數據?
謝謝您的幫助。
Class document:
public partial class Document
{
public Document()
{
Services = new HashSet<Service>();
}
public Guid DocumentId { get; set; }
public string? DocTitre { get; set; }
public virtual ICollection<Service> Services { get; set; }
}
public partial class Service
{
public Service()
{
Docs = new HashSet<Document>();
}
public Guid ServiceId { get; set; }
public string? Libelle { get; set; }
public virtual ICollection<Document> Docs { get; set; }
}
我找到了如何獲取數據的答案:
var services = await _context.Services
.Where(s => s.ServiceId == Id)
.Include(s =>s.Docs)
.ToListAsync();
return services;
謝謝你。
var result = await _dbContext.BillingGroupFakes .Where(b => b.Customers.FirstOrDefault().ExternalCustomerId.Equals($"{id}")) .Include(b => b.Customers) .Select(m => new { m.Customers.FirstOrDefault().CustomerId, CustomerName = $"{m.Customers.FirstOrDefault().CustomerLastName}, {m.Customers.FirstOrDefault().CustomerName}", m.BillingGroupId, m.BillingGroupCode, m.BillingGroupDescription }) .AsNoTracking() .ToListAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.