explicit many to many relation

This commit is contained in:
Фролов 2021-07-21 16:30:57 +05:00
parent 6abbf476b1
commit beab5a39a1
7 changed files with 72 additions and 28 deletions

View File

@ -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<Well>(d => d.IdTelemetry)
@ -178,6 +174,21 @@ namespace AsbCloudDb.Model
});
modelBuilder.Entity<RelationCompanyWell>(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<RelationCompanyWell>(entity => {
entity.HasData(new List<RelationCompanyWell> {
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<Operation>(entity =>
{
entity.HasData(new List<Operation> {
@ -243,10 +268,11 @@ namespace AsbCloudDb.Model
public IQueryable<Well> 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;
}

View File

@ -10,12 +10,6 @@ namespace AsbCloudDb.Model
[Table("t_company")]
public partial class Company : IId
{
public Company()
{
Users = new HashSet<User>();
Wells = new HashSet<Well>();
}
[Key]
[Column("id")]
public int Id { get; set; }
@ -35,7 +29,7 @@ namespace AsbCloudDb.Model
[InverseProperty(nameof(User.Company))]
public virtual ICollection<User> Users { get; set; }
[InverseProperty(nameof(Well.Companies))]
public virtual ICollection<Well> Wells { get; set; }
[InverseProperty(nameof(RelationCompanyWell.Company))]
public virtual ICollection<RelationCompanyWell> RelationCompaniesWells { get; set; }
}
}

View File

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

View File

@ -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<WellSection> Sections { get; set; }
[InverseProperty(nameof(Company.Wells))]
public virtual ICollection<Company> Companies { get; set; }
[InverseProperty(nameof(RelationCompanyWell.Well))]
public virtual ICollection<RelationCompanyWell> RelationCompaniesWells { get; set; }
[ForeignKey(nameof(IdCluster))]
[InverseProperty(nameof(Model.Cluster.Wells))]

View File

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

View File

@ -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)

View File

@ -93,10 +93,7 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<WellDrillingStatDto> 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,
});