多对多实体框架核心6(未生成交界表)

发布于 2025-01-20 12:10:12 字数 1204 浏览 2 评论 0原文

我刚刚开始使用 Entity Framework Core 6。 我正在使用一个示例数据库,其中存在多对多关系。

我在 SQL Server 上创建了数据库。我创建了三个表: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; }
    }

这里有一些截图: 数据库图表 文档

服务

I've just started to work with Entity Framework Core 6.
I am working with a sample database where I have a many to many relationship.

I created my database on SQL server. I created three tables: Service, Document, ServiceDocs (used as a Junction Table).

Then I did :

scaffolf-dbcontext

both classes have been generated except the junction table ServiceDocs.
My question is: How can I add elements to the junction table and get data from it without the class of the junction table?

Thank you for your help.

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; }
    }

Here some screenshots :
Database diagram
Document

Service

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2025-01-27 12:10:12

我找到了如何获取数据的答案:

var services = await _context.Services
  .Where(s => s.ServiceId == Id)
  .Include(s =>s.Docs)
  .ToListAsync();

return services;

谢谢。

I found the answer how to get the data:

var services = await _context.Services
  .Where(s => s.ServiceId == Id)
  .Include(s =>s.Docs)
  .ToListAsync();

return services;

Thank you.

还不是爱你 2025-01-27 12:10:12
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();

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文