我刚刚开始使用实体框架核心6。我正在使用一个示例数据库,其中我有很多到许多关系。
我在SQL server上创建了数据库。我创建了三个表:服务、文档、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; }
}发布于 2022-06-23 12:27:49
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();
发布于 2022-04-13 12:03:37
我找到了如何获得数据的答案:
var services = await _context.Services
.Where(s => s.ServiceId == Id)
.Include(s =>s.Docs)
.ToListAsync();
return services;谢谢。
https://stackoverflow.com/questions/71829025
复制相似问题