Oto sytuacja. Istnieją dwa typy ElectricConsumer
, tj. CommercialConsumers
& DomesticConsumers (Quaters) i jeden Quater
jest przydzielony do jednego Employee
. Poniżej znajduje się mój kod, ale napotykam wyjątek.Nie można określić głównego końca powiązania między typami
Nie można określić główne koniec związku między typami EFcodefirstDemo.CodeFistModel.Quater i EFcodefirstDemo.CodeFistModel.Employee. Główny koniec tego powiązania musi być jawnie skonfigurowany przy użyciu płynnego API relacji lub adnotacji danych z poziomu .
Wiem, że popełniam błędy w niektórych miejscach, ponieważ jestem nowy w EF. Mam nadzieję, że rozwiążesz ten problem.
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; }
}
Co zrobić w przypadku jednego-to- (zero lub jeden)? –
Zleceniodawca jest wymagany w jednostce zależnej, więc podczas tworzenia instancji podmiotu zależnego należy ustawić właściwość navigation z instancją głównego. Jeśli tworzysz instancję głównego, nie musisz ustawiać zależnej właściwości nawigacji, więc w tym przypadku główny może istnieć bez zależnej, więc na końcu jest to relacja jeden-zero-jeden – octavioccl