Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want a one-to-one relationship, between route and difficult level(simple, hard)

I have these models:

C#
public class Route
    {

        [Key]
        public int routeID { get; set; }
        public string Name { get; set; }

        public int? UserProfileID { get; set; }
        public int? CountryID { get; set; }
        //public int? DifficultGradeID { get; set; }



        public virtual UserProfile userProfile { get; set; }
        public virtual Country country { get; set; }
        public virtual Difficult difficult { get; set; }
    }



public class Difficult
    {
        [Key, ForeignKey("Route")]
        public int routeID { get; set; }
        public string DifficultName { get; set; }

        public virtual Route route { get; set; }

    }


but every time I do Update-Databse, I get this error:

Quote:
The ForeignKeyAttribute on property 'routeID' on type 'ContosoUniversity.Models.Difficult' is not valid. The navigation property 'Route' was not found on the dependent type 'ContosoUniversity.Models.Difficult'. The Name value should be a valid navigation property name.


Thank you
Posted
Updated 21-Feb-15 8:20am
v2

1 solution

You just need to make the foreign key name match the navigation property.
In the foreign key, you have used "Route", and the navigation property is "route"
So change this:
C#
[Key, ForeignKey("Route")]

to this:
C#
[Key, ForeignKey("route")]

And you should be sorted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900