I have an existing database with a very simple one-way foreign key relationship between two tables. I need to create classes with mappings that will work with an existing database.
CREATE TABLE OfflinePackage ( PackageID int, Name nvarchar(100) ... CREATE TABLE OfflinePackageDocument ( PackageDocumentID int, PackageID int,
I define a class for each of these tables. I want to use annotations (or a free api if I need) to map a foreign key. OfflinePackageDocument.PackageID to OfflinePackage.PackageID
I am trying to use RelatedTo annotation:
public class OfflinePackageDocument { [Key, StoreGenerated(StoreGeneratedPattern.Identity)] public int PackageDocumentID { get; set; } [RelatedTo(Property = "PackageID")] public virtual OfflinePackage Package { get; set; } ... } public class OfflinePackage { [Key, StoreGenerated(StoreGeneratedPattern.Identity)] public int PackageID { get; set; } ... }
But I get this error during ModelBuilder.CreateModel ():
The navigation property 'PackageID', which was specified with the value RelatedToAttribute.Property, cannot be found in the corresponding type 'OfflinePackage'.
PackageID is certainly a property off-line.
I canβt understand what I am doing wrong here.
source share