I’ve got two tables:
- Documents(Id, DocumentTypeId, Title, Details)
- DocumentTypes (Id, Name, Description).
DocumentTypeId is a foreign key that refers to DocumentTypes table. I.e. all documents can should
have a type assigned to them.
I’ve got two classes:
public class Document
{
public string Id { get; set; }
public string Title { get; set; }
public DocumentType DocumentType { get; set; }
}
and
public class DocumentType
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
and I’ve got a configuration
internal class DocumentsConfiguration : EntityTypeConfiguration<Document>
{
public DocumentsConfiguration()
{
ToTable("Documents");
HasKey(document => document.Id);
Property(document => document.Id).HasColumnName("Id");
HasRequired(document => document.DocumentType);//????????
Property(document => document.Title).HasColumnName("Title").IsRequired();
}
}
And this is not working. I’m getting this error message:
Invalid column name 'DocumentType_Id'
If I rename the fk column to be DocumentType_Id then I’m getting this error message:
Invalid column name 'DocumentTypeId'
My question is how do I set such one-to-many relation? I.e. I’d like to have many documents with different document types.
First make this change. Navigation properties have to be
virtual:Then change your configuration to this:
You don’t need the
HasKeyorPropertycalls for theIdfield because they are already assumed by convention. Your table must have a columnDocumentIdin this configuration.