Unable to determine the main end of the relationship between types

Here is the situation. There are two types of ElectricConsumer i.e. CommercialConsumers , and DomesticConsumers (Quaters) and one Quater allocated to one Employee . Below is my code, but it contains an exception.

Unable to determine the main end of the relationship between the types EFcodefirstDemo.CodeFistModel.Quater and EFcodefirstDemo.CodeFistModel.Employee. The main end of this association must be explicitly configured using freely available APIs or data annotations.

I know I'm making mistakes somewhere because I'm new to EF. Hope you resolve 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; } } 
+6
source share
1 answer

This exception is thrown because you are trying to set up a one-to-one relationship , but you are not specifying which end is the main one in this regard. The main end is the one that will be inserted first and which can exist without a dependent. The dependent end is the one that must be inserted after the principal, because it has a foreign key for the principal. To solve this problem, you can use Required data annotation (I assume the Quater object is dependent in this case):

 public partial class Quater : ElectricConsumer { //... [Required] public virtual Employee Employee { get; set; } } 
+10
source

Source: https://habr.com/ru/post/983400/


All Articles