I have a program written with a database-first approach; I have a table ServicePlan and another ServicePlanDetails. They are not mapped to each other, but they have a common column PlanId; a servicePlan can contain multiple ServicePlanDetails like a list of it.
I don't want to make any change to the database, but I want to map them as well. How can I do this? Does doing this within the method of on model creating will do the work for me and will not change anything in the database? I have tried this but could get the result.
For simplicity, I have just added few columns and their mapping and not all of them:
public partial class ServicePlan
{
public ServicePlan()
{
ServicePlanDetails = new HashSet<ServicePlanDetail>();
}
public long PlanId { get; set; }
public decimal PhoneId { get; set; }
public byte? NLines { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public virtual ICollection<ServicePlanDetail> ServicePlanDetails { get; set; }
}
public partial class ServicePlanDetail
{
public long PlanId { get; set; }
public string? ServCode { get; set; }
public string? CountryCode { get; set; }
public bool? IsPlan { get; set; }
public decimal? Cost { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ServicePlan>(entity =>
{
entity.HasKey(e => e.PlanId).HasName("PK_UsersPlan");
entity.ToTable("ServicePlan");
entity.HasIndex(e => e.VideoTronId, "IDX_VTID").HasFillFactor(80);
entity.HasIndex(e => new { e.PhoneId, e.IsApproved }, "Ix_SrvcPlan").HasFillFactor(80);
entity.Property(e => e.Zone).HasMaxLength(50);
entity.HasMany(p => p.ServicePlanDetails)
.WithOne()
.HasPrincipalKey(p => p.PlanId)
.HasForeignKey(p => p.PlanId);
});
}
The error I get is :
Unable to determine the relationship represented by navigation 'ServicePlan.ServicePlanDetails' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
I want to get the serviceplandetails with the same planid as the serviceplan into a list in the serviceplan.