I have an existing DB with a very simple one-way foreign key relationship between two tables. I need to create classes with mappings that will work with the existing DB.
CREATE TABLE OfflinePackage (
PackageID int,
Name nvarchar(100)
...
CREATE TABLE OfflinePackageDocument (
PackageDocumentID int,
PackageID int, --- FK references OfflinePackage.PackageID
...
I define a class for each of these tables. I want to use annotations (or fluent api if I have to) to map the foreign key OfflinePackageDocument.PackageID to OfflinePackage.PackageID
I'm trying to use the 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' that was specified by the RelatedToAttribute.Property value cannot be found on the related type 'OfflinePackage'.
PackageID certainly is a property in OfflinePackage.
I can't figure out what I'm doing wrong here.