0
namespace Healthcare
{
    public class Doc
    {
        public int DocId { get; set; }
        public string DocName { get; set; }
        public string Street { get; set; }
        public int Number { get; set; }
        public string Zip { get; set; }
        public string Place { get; set; }

        public List<DocContract> DocContract { get; set; }
    }

    public class DocContract
    {
        public int DocId { get; set; }
        public string Date { get; set; }
        public Doc Doc { get; set; }
    }
}

Whenever I try to enable migrations Im getting the following error:

Healthcare.DocContract: : EntityType 'DocContract' has no key defined. Define the key for this EntityType.

How can I solve this error? Basically every doc has one contract

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Khay
  • 9
  • 2

1 Answers1

2

Here one-to-one relation is shown, where DocContract is dependent:

public class Doc
{
    public int DocId { get; set; }
    public string DocName { get; set; }
    public string Street { get; set; }
    public int Number { get; set; }
    public string Zip { get; set; }
    public string Place { get; set; }

    public virtual DocContract DocContract { get; set; }
}

public class DocContract
{
    [Key]
    [ForeignKey("Doc")]
    public int DocId { get; set; }
    public string Date { get; set; }
    public virtual Doc Doc { get; set; }
}
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26