c# - 无法确定类型之间关联的主体端

标签 c# entity-framework ef-code-first one-to-one ado.net-entity-data-model

<分区>

情况是这样的。 ElectricConsumer 有两种类型,即 CommercialConsumers 和 DomesticConsumers(Quaters),一个 Quater 分配给一个 Employee。 下面是我的代码,但遇到异常。

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.

我知道我在某些地方犯了错误,因为我是 EF 的新手。希望你能解决这个问题。

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

最佳答案

启动该异常是因为您正在尝试配置一对一关系,但您没有指定哪一端是关系中的主体。 Principal 端是最先插入的端,可以在没有从属端的情况下存在。 Dependent 端是必须插入主体之后的一端,因为它有主体的外键。要解决此特定问题,您可以使用 Required数据注释(我假设 Quater 实体在这种情况下是依赖项):

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

关于c# - 无法确定类型之间关联的主体端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28887156/

相关文章:

c# - 如何对 EF Core 2.1 中的继承列进行排序?

c# - 什么是 SqlExpressionVisitor

entity-framework - 如何判断实体集合中的关系是否已经保存到数据库中

entity-framework - LINQ To Entities + Include + 匿名类型问题

c# - 如何调整 ObservableCollection 的大小?

c# - 使用 Epplus 的条件格式更改单元格颜色

entity-framework - Entity Framework 多对多CRUD

c# - 网站 | Entity Framework ,竞争条件

c# - Entity Framework 和多个模式

c# - 实体或复杂类型 'x' 无法在 linq to entities 查询中构建