Foreign Key issue with Code first from Existing Database

mcalex

tl;dr Do I need to have a foreign key id field as a property in the related class with EF code-first?


Following ScottGu's advice, i have created a model to reflect an existing database. Two of the tables in the db are: Project and ProjectType with a foreign key relationship. Each Project has a ProjectType.

I have added the necessary virtual fields to the model classes to reflect the relationship:

public class Project {
  public int ProjectID { get; set; }
  public string ProjectName { get; set; }
  public DateTime CreationDate { get; set; }
  ...
  public virtual ProjectType ProjectType {get; set; }
  ...
}

public class ProjectType {
  public int ProjectTypeID { get; set; }
  ...
  public virtual ICollection<Project> Projects { get; set;}
  ...
}

According to Scott (as shown in the image below), there is no need for the actual (foreign key) ID field to be exposed in the dependent class, ie, I don't need a public int ProjectTypeID { get; set; } property in the Project class.

Model definition

However, when I try a call to retrieve the data, I hit an EntityCommandExecutionException with an InnerException of: Invalid column name 'ProjectType_ProjectTypeID'

Initial googling suggested adding a [ForeignKey] annotation to the ProjectType property. I tried both [ForeignKey("ProjectType")] and [ForeignKey("ProjectTypeID")] but neither worked.

Further googling suggested using FluentAPI with a call to:

modelBuilder.Entity<Project>().HasRequired<ProjectType>(p => p.ProjectType)
                              .WithMany(pt => pt.Projects)

in an OnModelCreating method, but this falls over with the same Invalid column name error.

So, do I need to have the ProjectTypeID field as a property in the Project class? If not, how do I tell EF to use the ProjectTypeID as the foreign key?

Alaa Masoud

What is the foreign key column name in the existing database? You don't need to add a foreign key field but you do need to configure your modelBuilder so that foreign key names match.

modelBuilder.Entity<Project>()
            .HasRequired<ProjectType>(p => p.ProjectType)
            .WithMany(pt => pt.Projects)
            .Map(p => p.MapKey("FK_NAME_IN_EXISTING_DB"));

You can also choose the option to have EF generate code first from database.

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 Foreign Key issue

From Dev

Foreign Key from an existing database with Persistent Haskell

From Dev

Foreign Key from an existing database with Persistent Haskell

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Circular foreign key code first

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Use existing database entry for foreign key in Doctrine

From Dev

How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

From Dev

Updating EF entities that are "code first from an existing database"

From Dev

EF 6 Code First From Database - Calling existing stored procedure

From Dev

code first to existing database missing from wizard selection

From Dev

Updating EF entities that are "code first from an existing database"

From Dev

Code First migration from int to Guid primary key issue

From Dev

Django model foreign key from existing table

From Dev

Entity Framework code first can't update database after setting foreign key as nullable

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

Code first foreign key association MVC Entity

From Dev

EF Code First Foreign Key Same Table

From Dev

EF Code First foreign key with multiple keys

From Dev

Foreign Key in Code First Entity Framework

From Dev

Foreign Key with Fluent API - Code First

From Dev

Entity Framework: Foreign Key in code first

From Dev

Code first approach not creating foreign key

From Dev

EF Code First Foreign Key Relationship

From Dev

Entity Framework Code First from database not adding Key attribute

From Dev

JSF Form From Entity and Foreign Key Issue

From Dev

Entity Framework one to many with a foreign key of a different type and existing database

From Dev

Add Table in Existing Code First Database Approach

Related Related

  1. 1

    Entity Framework Code First Foreign Key issue

  2. 2

    Entity Framework Code First Foreign Key issue

  3. 3

    Foreign Key from an existing database with Persistent Haskell

  4. 4

    Foreign Key from an existing database with Persistent Haskell

  5. 5

    Entity Framework Code First and Firebird - Foreign Key name issue

  6. 6

    Circular foreign key code first

  7. 7

    Exchanged Foreign Key on Entity Framework Code First From data base

  8. 8

    Use existing database entry for foreign key in Doctrine

  9. 9

    How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

  10. 10

    Updating EF entities that are "code first from an existing database"

  11. 11

    EF 6 Code First From Database - Calling existing stored procedure

  12. 12

    code first to existing database missing from wizard selection

  13. 13

    Updating EF entities that are "code first from an existing database"

  14. 14

    Code First migration from int to Guid primary key issue

  15. 15

    Django model foreign key from existing table

  16. 16

    Entity Framework code first can't update database after setting foreign key as nullable

  17. 17

    Disable Foreign Key Constraint Code First EF

  18. 18

    Code first foreign key association MVC Entity

  19. 19

    EF Code First Foreign Key Same Table

  20. 20

    EF Code First foreign key with multiple keys

  21. 21

    Foreign Key in Code First Entity Framework

  22. 22

    Foreign Key with Fluent API - Code First

  23. 23

    Entity Framework: Foreign Key in code first

  24. 24

    Code first approach not creating foreign key

  25. 25

    EF Code First Foreign Key Relationship

  26. 26

    Entity Framework Code First from database not adding Key attribute

  27. 27

    JSF Form From Entity and Foreign Key Issue

  28. 28

    Entity Framework one to many with a foreign key of a different type and existing database

  29. 29

    Add Table in Existing Code First Database Approach

HotTag

Archive