Page 1 of 1

Entity Framework Core - Data annotation foreign keys are not generated?

Posted: Wed 12 Apr 2017 04:44
by RobertK
For Foreign keys columns: When you generate a class model with EFCore template, the foreign key property generated looks something like this

Code: Select all

public virtual long CountryId { get; set; }
Is it missing a foreign key data annotation or a databasegenerated data annotation?

Note: this is validation set to "data annotation" foreign keys just don't get generated?

Re: Entity Framework Core - Data annotation foreign keys are not generated?

Posted: Wed 12 Apr 2017 17:48
by Shalex
The EF Core template generates fluent mapping for all classes and its properties (including foreign keys) in ModelName.cs.

If you want to add custom attributes:
1) double click diagram surface to open Model Settings and navigate to Model > Settings > Attributes > select the assembly (e.g.: System.ComponentModel.DataAnnotations.dll) with the needed attributes and make sure that the needed attributes (e.g.: ForeignKeyAttribute) are checked in the window below, press OK
2) select the needed attribute in the Attributes collection of a particular class property