Entity Framework Code First Foreign Key issue

BJladu4

I have an EF Code First test app. I want to make one-to-many associations between 3 tables. I want to make a schema which looks like this http://gyazo.com/7a1800230a3838adecaafc5cb6676b25.png. When i launch my app VS says me:

The ForeignKeyAttribute on property 'EducationLevels' on type 'ConsoleApplication2.Employee' is not valid. The foreign key name 'EducationLevelId' was not found on the dependent type 'ConsoleApplication2.EducationLevel'. The Name value should be a comma separated list of foreign key property names.

Here it is my code:

class Program
{
    static void Main(string[] args)
    {
        using (EmployeesContext context = new EmployeesContext())
        {                
            Profession p = new Profession { Id = 0, NameOfProfession = "myprof" };
            context.Profession.Add(p);
            context.SaveChanges();
        }
    }
}

public enum Sex { Man = 0, Woman = 1 }

public class Employee
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public byte Age { get; set; }
    public Sex Sex { get; set; }
    public int EducationLevelId { get; set; }
    public int ProfessionId { get; set; }

    [ForeignKey("EducationLevelId")]
    public virtual ICollection<EducationLevel> EducationLevels { get; set; }
    [ForeignKey("ProfessionId")]
    public virtual ICollection<Profession> Professions { get; set; }
}

public class EducationLevel
{
    [Key]
    public int Id { get; set; }
    public string Level { get; set; }

    public virtual Employee Employees { get; set; }
}

public class Profession
{
    [Key]
    public int Id { get; set; }
    public string NameOfProfession { get; set; }

    public virtual Employee Employees { get; set; }
}

public class EmployeesContext : DbContext
{
    public DbSet<Employee> Employee { get; set; }
    public DbSet<EducationLevel> EducationLevel { get; set; }
    public DbSet<Profession> Profession { get; set; }
}
Slauma

You need to swap collection and reference navigation properties (an Employee has one EducationLevel and one Profession, not many, and an EducationLevel has many Employees and not one, and a Profession has many Employees and not one):

public class Employee
{
    // ...
    public int EducationLevelId { get; set; }
    public int ProfessionId { get; set; }

    [ForeignKey("EducationLevelId")]
    public virtual EducationLevel EducationLevel { get; set; }
    [ForeignKey("ProfessionId")]
    public virtual Profession Profession { get; set; }
}

public class EducationLevel
{
    // ...

    public virtual ICollection<Employee> Employees { get; set; }
}

public class Profession
{
    // ...

    public virtual ICollection<Employee> Employees { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, get access to foreign key value

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Entity Framework 6 - foreign key issue

From Dev

Entity Framework 6 Foreign Key Issue

From Dev

Code First entity framework and foreign keys

From Dev

Entity Framework Code First initializing foreign keys

From Dev

Code first foreign key association MVC Entity

From Dev

Include foreign key in composite primary key in Code-First Entity Framework

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

From Dev

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

From Dev

Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

From Dev

Entity framework 6 code first saving collection via foreign key relation

Related Related

  1. 1

    Entity Framework Code First Foreign Key issue

  2. 2

    Entity Framework Code First and Firebird - Foreign Key name issue

  3. 3

    Foreign Key in Code First Entity Framework

  4. 4

    Entity Framework: Foreign Key in code first

  5. 5

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  6. 6

    Entity Framework Code First Foreign Key adding Index as well

  7. 7

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  8. 8

    Entity Framework Code first adds unwanted foreign key column

  9. 9

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  10. 10

    Multiple Foreign Key for Same table in Entity Framework Code First

  11. 11

    Get foreign key value using Entity Framework code first

  12. 12

    Entity Framework one to optional foreign key code first fluent mapping

  13. 13

    Entity framework code first cant create primary and foreign key relationship

  14. 14

    Entity framework code first, get access to foreign key value

  15. 15

    Entity framework code first, custom foreign key name for inheritance

  16. 16

    How to specify a foreign key with code-first Entity Framework

  17. 17

    Exchanged Foreign Key on Entity Framework Code First From data base

  18. 18

    Entity Framework 6 - foreign key issue

  19. 19

    Entity Framework 6 Foreign Key Issue

  20. 20

    Code First entity framework and foreign keys

  21. 21

    Entity Framework Code First initializing foreign keys

  22. 22

    Code first foreign key association MVC Entity

  23. 23

    Include foreign key in composite primary key in Code-First Entity Framework

  24. 24

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  25. 25

    Entity Framework 6 multiple table to one foreign key relationship code first

  26. 26

    Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

  27. 27

    Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

  28. 28

    Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

  29. 29

    Entity framework 6 code first saving collection via foreign key relation

HotTag

Archive