diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 73efec00..2055d509 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -166,10 +166,6 @@ namespace AsbCloudDb.Model .HasForeignKey(d => d.IdCluster) .HasConstraintName("t_well_t_cluster_id_fk"); - // Todo: Setup relation table names (https://stackoverflow.com/questions/11769864/many-to-many-relationships-in-ef5-code-first-how-can-i-specify-table-name) - entity.HasMany(d => d.Companies) - .WithMany(p => p.Wells); - entity.HasOne(d => d.Telemetry) .WithOne(p => p.Well) .HasForeignKey(d => d.IdTelemetry) @@ -178,6 +174,21 @@ namespace AsbCloudDb.Model }); + modelBuilder.Entity(entity => { + + entity.HasKey(nameof(RelationCompanyWell.IdCompany), nameof(RelationCompanyWell.IdWell)); + + entity.HasOne(r => r.Well) + .WithMany(w => w.RelationCompaniesWells) + .HasForeignKey(r => r.IdWell) + .HasConstraintName("t_relation_company_well_t_well_id_fk"); + + entity.HasOne(r => r.Company) + .WithMany(w => w.RelationCompaniesWells) + .HasForeignKey(r => r.IdCompany) + .HasConstraintName("t_relation_company_well_t_company_id_fk"); + }); + FillData(modelBuilder); } @@ -216,6 +227,20 @@ namespace AsbCloudDb.Model }); }); + modelBuilder.Entity(entity => { + entity.HasData(new List { + new RelationCompanyWell{ IdWell = 1, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 2, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 3, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 4, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 5, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 6, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 7, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 8, IdCompany = 1}, + new RelationCompanyWell{ IdWell = 9, IdCompany = 1}, + }); + }); + modelBuilder.Entity(entity => { entity.HasData(new List { @@ -243,10 +268,11 @@ namespace AsbCloudDb.Model public IQueryable GetWellsForCompany(int idCompany) { return from well in Wells - .Include(w => w.Companies) + .Include(w => w.RelationCompaniesWells) + .ThenInclude(r => r.Company) .Include(w => w.Cluster) .ThenInclude(c => c.Deposit) - where well.Companies.Any(c => c.Id == idCompany) + where well.RelationCompaniesWells.Any(c => c.IdCompany == idCompany) select well; } diff --git a/AsbCloudDb/Model/Company.cs b/AsbCloudDb/Model/Company.cs index 98269519..3c283781 100644 --- a/AsbCloudDb/Model/Company.cs +++ b/AsbCloudDb/Model/Company.cs @@ -10,12 +10,6 @@ namespace AsbCloudDb.Model [Table("t_company")] public partial class Company : IId { - public Company() - { - Users = new HashSet(); - Wells = new HashSet(); - } - [Key] [Column("id")] public int Id { get; set; } @@ -35,7 +29,7 @@ namespace AsbCloudDb.Model [InverseProperty(nameof(User.Company))] public virtual ICollection Users { get; set; } - [InverseProperty(nameof(Well.Companies))] - public virtual ICollection Wells { get; set; } + [InverseProperty(nameof(RelationCompanyWell.Company))] + public virtual ICollection RelationCompaniesWells { get; set; } } } diff --git a/AsbCloudDb/Model/RelationCompanyWell.cs b/AsbCloudDb/Model/RelationCompanyWell.cs new file mode 100644 index 00000000..efcf6aac --- /dev/null +++ b/AsbCloudDb/Model/RelationCompanyWell.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +#nullable disable + +namespace AsbCloudDb.Model +{ + [Table("t_relation_company_well"), Comment("отношение скважин и компаний")] + public partial class RelationCompanyWell + { + [Column("id_well")] + public int IdWell { get; set; } + + [Column("id_company")] + public int IdCompany { get; set; } + + [ForeignKey(nameof(IdWell))] + [InverseProperty(nameof(Model.Well.RelationCompaniesWells))] + public virtual Well Well { get; set; } + + [ForeignKey(nameof(IdCompany))] + [InverseProperty(nameof(Model.Company.RelationCompaniesWells))] + public virtual Company Company { get; set; } + } +} diff --git a/AsbCloudDb/Model/Well.cs b/AsbCloudDb/Model/Well.cs index ad3a3275..b57efb90 100644 --- a/AsbCloudDb/Model/Well.cs +++ b/AsbCloudDb/Model/Well.cs @@ -61,7 +61,6 @@ namespace AsbCloudDb.Model [Column("route_speed_fact"), Comment("Рейсовая скорость факт")] public double? RouteSpeedFact { get; set; } - [ForeignKey(nameof(IdWellType))] [InverseProperty(nameof(Model.WellType.Wells))] public virtual WellType WellType { get; set; } @@ -69,8 +68,8 @@ namespace AsbCloudDb.Model [InverseProperty(nameof(WellSection.Well))] public virtual ICollection Sections { get; set; } - [InverseProperty(nameof(Company.Wells))] - public virtual ICollection Companies { get; set; } + [InverseProperty(nameof(RelationCompanyWell.Well))] + public virtual ICollection RelationCompaniesWells { get; set; } [ForeignKey(nameof(IdCluster))] [InverseProperty(nameof(Model.Cluster.Wells))] diff --git a/AsbCloudInfrastructure/ReportDataSourcePgCloud.cs b/AsbCloudInfrastructure/ReportDataSourcePgCloud.cs index 11d605d3..0c7bb846 100644 --- a/AsbCloudInfrastructure/ReportDataSourcePgCloud.cs +++ b/AsbCloudInfrastructure/ReportDataSourcePgCloud.cs @@ -29,7 +29,8 @@ namespace AsbSaubReport var well = context.Wells .Include(w => w.Cluster) .ThenInclude(c => c.Deposit) - .Include(w => w.Companies) + .Include(w => w.RelationCompaniesWells) + .ThenInclude(r => r.Company) .Include(w => w.Telemetry) .FirstOrDefault(w => w.Id == wellId); @@ -50,7 +51,7 @@ namespace AsbSaubReport Deposit = well.Cluster.Deposit.Caption, Cluster = well.Cluster.Caption, Well = well.Caption, - Customer = well.Companies.First(c => c.IdCompanyType == 1)?.Caption, + Customer = well.RelationCompaniesWells.First(c => c.Company.IdCompanyType == 1)?.Company.Caption, DrillingStartDate = well.Telemetry?.Info?.DrillingStartDate ?? default, TimeZoneId = well.Telemetry?.Info?.TimeZoneId ?? default, }; diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 30cfbd86..2192bbb6 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -41,7 +41,7 @@ namespace AsbCloudInfrastructure.Services } public bool CheckWellOwnership(int idCompany, int wellId) - => cacheWells.Contains(w => w.Id == wellId && (w.Companies.FirstOrDefault(c => c.Id == idCompany) != null)); + => cacheWells.Contains(w => w.Id == wellId && w.RelationCompaniesWells.Any(r => r.IdCompany == idCompany)); private static WellDto From(Well well) diff --git a/AsbCloudInfrastructure/Services/СlusterService.cs b/AsbCloudInfrastructure/Services/СlusterService.cs index 5721b98c..20e7e230 100644 --- a/AsbCloudInfrastructure/Services/СlusterService.cs +++ b/AsbCloudInfrastructure/Services/СlusterService.cs @@ -93,10 +93,7 @@ namespace AsbCloudInfrastructure.Services public IEnumerable GetStat(int idCompany, int idCluster) { var entities = from w in db.Wells - .Include(e => e.Companies) - .Include(e => e.Sections) - - where w.IdCluster == idCluster && w.Companies.Any(c => c.Id == idCompany) + where w.IdCluster == idCluster && w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany) select w; var dtos = entities.Select(e => new WellDrillingStatDto @@ -135,11 +132,11 @@ namespace AsbCloudInfrastructure.Services WellDepthPlan = s.WellDepthPlan, }), UnProductiveDays = e.UnProductiveDays, - Companies = e.Companies.Select(c => new CompanyDto + Companies = e.RelationCompaniesWells.Select(c => new CompanyDto { - Id = c.Id, - Caption = c.Caption, - CompanyType = c.CompanyType.Caption, + Id = c.Company.Id, + Caption = c.Company.Caption, + CompanyType = c.Company.CompanyType.Caption, }), WellType = e.WellType.Caption, });