简体   繁体   English

多对多 Entity Framework Core 6(未生成联结表)

[英]Many-to-Many Entity Framework Core 6 (junction table not generated)

I've just started to work with Entity Framework Core 6 .我刚刚开始使用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.我在 SQL 服务器上创建了我的数据库。 I created three tables: Service, Document, ServiceDocs (used as a Junction Table).我创建了三个表:Service、Document、ServiceDocs(用作连接表)。

Then I did :然后我做了:

scaffolf-dbcontext

both classes have been generated except the junction table ServiceDocs.除了联结表 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服务

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.谢谢你。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM