Unable to determine the principal end of an association between the types [duplicate]
Asked Answered
M

1

12

Here is the situation. There are two type of ElectricConsumer ie CommercialConsumers & DomesticConsumers(Quaters) and one Quater is allocated to one Employee. Below is my code but encountring the exception .

Unable to determine the principal end of an association between the types EFcodefirstDemo.CodeFistModel.Quater and EFcodefirstDemo.CodeFistModel.Employee. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I know i am making mistakes some where because i am new to EF. Hope You Will Solve This Issue .

public class Employee
{
    public Employee()
    {
        MeterReadings = new List<MeterReading>();
        MeterReadings = new List<MeterReading>();
    }

    [Key]
    [Column(Order = 1)]

    public int EmployeeID { get; set; }
    [Key]
    [Column(Order = 2)]

    public Int64 EmployeeNo { get; set; }
    public String EmployeeName { get; set; }
    [DefaultValue(true)]
    public bool Gender { get; set; }
    [DefaultValue(true)]

    public bool HasResidence { get; set; }
    public bool IsInDivision { get; set; }

    public int? ManagerID { get; set; }
    public virtual Employee Manager { get; set; }


    public virtual Department Deparment { get; set; }
    public int QuaterID { get; set; }
    [ForeignKey("QuaterID")]
     public virtual Quater Quater { get; set; }

    public virtual ICollection<MeterReading> MeterReadings { get; set; }

}

public partial class ElectricConsumer
{
    [Key]
    public int ElectricConsumerID { get; set; }
    public String Area { get; set; }
    [MaxLength(350)]
    public String Address { get; set; }
    public virtual ICollection< Meter> Meters { get; set; }
}




public partial class Quater : ElectricConsumer
{

    public Quater()
    {
        //  Meters = new List<Meter>();
    }

    public int QuaterNo { get; set; }
    public int QuaterPortionNo { get; set; }
    public virtual Employee Employee { get; set; }


}

public partial class CommericalCustomer : ElectricConsumer
{
    public CommericalCustomer()
    {
        //   Meters = new List<Meter>();
    }
      public String Name { get; set; }
    [Index("NicIndex", IsUnique = true)]
    public Int64 NIC { get; set; }

    public int ShopNo { get; set; }


}


public partial class Meter
{
    public Meter()
    {
        InstalledDate = DateTime.Now;
        MeterReadings = new List<MeterReading>();
        ElectricBills = new List<ElectricBill>();
    }

    [Key]
    [Column(Order = 1)]
    public int MeterID { get; set; }
    [Key]
    [Column(Order = 2)]
    public int MeterNo { get; set; }
    [DefaultValue(1)]
    public int Phase { get; set; }

    public DateTime InstalledDate { get; set; }
    [DefaultValue(true)]
    public bool IsOperating { get; set; }

    public virtual ElectricConsumer ElectricConsumer { get; set; }
    public virtual ICollection<MeterReading> MeterReadings { get; set; }
    public virtual ICollection<ElectricBill> ElectricBills { get; set; }
}
Mosher answered 5/3, 2015 at 20:31 Comment(0)
K
36

That exception is launched because you are trying to configure an one-to-one relationship but you are not specifying which end is the principal in the relationship. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal. To resolve this particular problem you can use the Required data annotation (I suppose the Quater entity is the dependent in this case):

public partial class Quater : ElectricConsumer
{   
  //...    
  [Required]
  public virtual Employee Employee { get; set; }
}
Killdeer answered 5/3, 2015 at 20:45 Comment(3)
What to do in case of one-to-(zero-or-one) ?Kessiah
The principal is the required in the dependent entity, so when you create an instance of the dependent entity you must set the navigation property with an instance of principal. If you create an instance of principal you don't need to set the dependent navigation property so, in this case the principal can exist without the dependent, so at the end that is an one-zero to one relationshipKilldeer
I was trying with ForeignKey but this here is the solution. To me a bit surprising but working.Dorsoventral

© 2022 - 2024 — McMap. All rights reserved.