EntityFramework 数据库表 如果是一对一的主键关系时要加【Required】,不然会出错Unable to determine the principal end of an association between the types
Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
public partial class TicketInventory
{
[Required]
public TicketPurchase TicketPurchase { get; set; }
}
表关系如下图: 采购表[TicketPurchase]、库存表[TicketInventory]关系:1对1
public TicketInventoryEntityTypeConfiguration()
{
this.ToTable("TicketInventory");
this.HasKey(c => c.ID).Property(p => p.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//this.HasKey(m => m.FK_TicketPurchase).Property(m => m.FK_TicketPurchase);
this.HasRequired(t => t.TicketPurchase).WithOptional(t => t.TicketInventory);
}